All Projects → KrofDrakula → preact-perf-profiler

KrofDrakula / preact-perf-profiler

Licence: other
A HOC to enable measuring rendering performance for Preact components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to preact-perf-profiler

preact-bind-group
Preact Component to Group Form fields onChange Events to a single Callback
Stars: ✭ 25 (+38.89%)
Mutual labels:  preact, preact-components
preact-views
📺 Named views for Preact, with easy-as-pie linking between them.
Stars: ✭ 39 (+116.67%)
Mutual labels:  preact, preact-components
preact-component-console
A console emulator for preact.
Stars: ✭ 29 (+61.11%)
Mutual labels:  preact, preact-components
preact-route-async
Easy asynchronous loading for your router components. For 440B.
Stars: ✭ 36 (+100%)
Mutual labels:  preact, preact-components
preact-mdc
material design components for preact using material-components-web sass styles (for live demo click the link below)
Stars: ✭ 23 (+27.78%)
Mutual labels:  preact, preact-components
preact-motion
A fork of React-Motion to be used with Preact
Stars: ✭ 28 (+55.56%)
Mutual labels:  preact, preact-components
preact-token-input
🔖 A text field that tokenizes input, for things like tags.
Stars: ✭ 57 (+216.67%)
Mutual labels:  preact, preact-components
preact-custom-scrollbars
⇅ Preact scrollbars component
Stars: ✭ 20 (+11.11%)
Mutual labels:  preact, preact-components
dl-model
Dragalia Lost Model Viewer
Stars: ✭ 30 (+66.67%)
Mutual labels:  preact
jasonformat.com
My blog
Stars: ✭ 18 (+0%)
Mutual labels:  preact
csgo-rcon-nodejs
A web panel to control a CS:GO server
Stars: ✭ 46 (+155.56%)
Mutual labels:  preact
weather-sucks
Weather App with Estonian Mood
Stars: ✭ 23 (+27.78%)
Mutual labels:  preact
apps
daily.dev application suite
Stars: ✭ 253 (+1305.56%)
Mutual labels:  preact
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+366.67%)
Mutual labels:  preact
scala-js-preact
Scala.js facade for the Preact JavaScript library
Stars: ✭ 25 (+38.89%)
Mutual labels:  preact
desvg
🌅 Converts SVG files into components 🌞
Stars: ✭ 79 (+338.89%)
Mutual labels:  preact
preact-source-learn
Preact+hook源码解析
Stars: ✭ 16 (-11.11%)
Mutual labels:  preact
spoken-word
Spoken Word
Stars: ✭ 46 (+155.56%)
Mutual labels:  preact
preact-polka-ssr
Preact SSR using Polka
Stars: ✭ 27 (+50%)
Mutual labels:  preact
theodorusclarence.com
💠 Personal website and blog made using Next.js, TypeScript, Tailwind CSS, MDX Bundler, FaunaDB, and Preact.
Stars: ✭ 205 (+1038.89%)
Mutual labels:  preact

preact-perf-profiler

Build Status

Ever wanted to measure the time spent rendering a component in the timeline, but soon got tired of manually sprinkling performance.mark() and performance.measure() everywhere?

You need this.

Demo

You can open the example URL as a standalone page to see how this looks in Dev Tools:

https://3826y5ky55.codesandbox.io/

Here's the full source:

Edit 3826y5ky55

Usage

withProfiler(AnyComponent [, name [, options]]) => Component

  • AnyComponent can be either a function that returns JSX or a class
  • name is optional and allows you to configure the measure name in the Performance tab
  • options is optional and allows you to pass a custom implementation for the Performance API

Configuring the measure name

The optional second argument allows you to pass a string or a function that the profiler will use to label the measure in the Performance tab. It will default to the function name if one is available.

// this uses MyComponent.name to generate the measure name
const ProfiledComponent = withProfile(MyComponent);

As a string

When you pass an anonymous function to the profiler, it's useful to also define a name for it:

const ProfiledComponent = withProfile(
  () => <p>Hello!</p>,
  'HelloComponent'
);

As a function

You can also pass a function as the second argument, which will be executed with the props of the component being rendered. This allows you to label the component in the context of its props.

const ProfiledUser = withProfile(
  User,
  ({ userId }) => `User #${userId}`
);

Walkthrough

Say you have a pure component that you'd like to measure:

// expensive_component.js
import { h } from 'preact';

const appendSum = (elements = [1], iterations = 10) => {
  const sum = elements.reduce((sum, next) => sum + next, 0);
  return elements.concat(sum);
};

const ExpensiveComponent = ({ iterations = 500 }) => {
  const sum = appendSum([1, 2], iterations);
  return (
    <div>
      <p>Whew, that was a lot of work, here are the results:</p>
      <ul>
        {sum.map(n => <li>{n}</li>)}
      </ul>
    </div>
  );
};

export default ExpensiveComponent;

To render it, just use the withProfiler HOC to wrap it:

import { h, render } from 'preact';
import withProfiler from 'preact-perf-profiler';
import ExpensiveComponent from './expensive_component';

const ProfiledExpensiveComponent = withProfiler(
  ExpensiveComponent,
  ({ iterations }) => `Expensive(${iterations})`
);

render(<ProfiledExpensiveComponent iterations={1000}/>, document.body);

Open the Performance tab and you'll now see each render of ProfiledExpensiveComponent in the measures in Dev Tools. The measure name will also be displayed as Expensive(1000).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].