npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loveholidays/preact-perf-metrics

v1.0.0

Published

Preact+Playwright performance test

Downloads

1,506

Readme

Overview

This utility allows you to instrument your preact application bundle, and to write performance tests using playwright to assert on the framework activity (rerenders, unmountings, etc) during an interaction.

Using this tool, you can ensure that performance doesn't regress unintentionally.

Know the story behind the tool in our Blog Post, and check the /example code for practical information.

Made with ❤️ by loveholidays.com


Usage

See an example application in the /example subdirectory.

1- Install the library

npm install @loveholidays/preact-perf-metrics

2- Instrument your application

import { setup } from "@loveholidays/preact-perf-metrics/lib/setup"

// ... somewhere in your app entry-point
setup();

[!NOTE] you might want to call setup() only in a testing environment (and not in the production build) because the instrumentation adds some overhead. Similarly to what you would do with preact/debug for the devtools.

3- Configure Playwright extension

This extends the expect function to support the libraries' custom matchers.

import { expect } from '@playwright/test';
import { extension } from '@loveholidays/preact-perf-metrics';
expect.extend(extension);

[!NOTE] This can also be done on the test file itself or in another file (see playwright-custom-matchers docs)

4- Write tests

Write performance tests using reset() (reset the counters) and the custom matchers (toPerform(), toRerender())

import { expect, test } from '@playwright/test';
import { reset } from '@loveholidays/preact-perf-metrics';

test("...", await ({page}) => {
    await page.goto('...');

    // prepare: clear the counters
    await reset(page);  /

    // action
    await page.getByText('Button').click();

    // test: use of custom matcher `toPerform`
    await expect(page).toPerform({ nodesRendered: 1, renderPhases: 1 });
})

API

The API can be divided in two:

  • to instrument the application that runs in the browser: setup(),
  • to write playwright tests: reset(), expect.toPerform(), and expect.toRerender()

setup()

Instruments the application to update the counters in window.__PREACT_PERF_METRICS__ upon any preact internal activity (renders, unmounting, etc).

reset()

Playwright utility to reset all the counters (accummulators) to zero. Used to set the beginning of a measurement.

expect().toPerform(...)

Playwright Custom matcher to assert on the framework activity during an interaction.

The parameter to assert is an object with one or many of the following keys:

interface Counters {
  nodesRendered?: number;
  renderPhases?: number;
  nodesUnmmouted?: number;
}

Example:

await reset();
await page.locator('.counter').click();
await expect(page).toPerform({ nodesRendered: 1 });

expect().toRerender(...)

Playwright Custom matcher to assert on the list of nodes rendered during an interaction.

The parameter to assert is an array of strings, with the names of the preact-nodes involved:

Example:

await reset();
await page.locator('.counter').click();
await expect(page).toRerender(["MyCounterPage", "Button"]);

Contributing

Please see our contributing guidelines


License

MIT