howslow
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

howslow

Easy performance testing in JavaScript!

npm i howslow
import howSlow from "howslow";

await howSlow("sum", (start, stop) => {
  start();
  let sum = 0;
  for (let i = 0; i < 1e7; i++) {
    sum += i;
  }
  stop();
});

To see how slow our code is, run the snippet above in node, deno or the browser. Timing statistics will be logged to the console:

sum

First run after start:
10.90 ms

Average of 50x after warm-up of 10x:
8.53 ± 0.12 ms

We have two different metrics which can both be important performance considerations: For example, "first run after start" can indicate how long something would take on page load. The warmed-up, averaged timing is relevant for code that runs often. The latter is usually much faster because it can benefit from compiler optimization.

Multiple timings

You can also have multiple related timings and give them labels:

import howSlow from "howslow";

await howSlow("json", (start, stop) => {
  let array = Array(1e6).fill(1);

  start("stringify");
  let json = JSON.stringify(array);
  stop();

  start("parse");
  array = JSON.parse(json);
  stop();
});

Which prints:

json

First run after start:
stringify:      35.13 ms
parse:          19.35 ms

Average of 50x after warm-up of 10x:
stringify:      25.30 ± 2.85 ms
parse:          14.20 ± 0.68 ms

Configuration

You can specify the number of timed runs and warm-up runs in an optional third argument:

howSlow("test", () => {}, {
  numberOfRuns: 200,
  numberOfWarmups: 20,
});

API

function howSlow(
  name: string,
  run: (
    start: (label?: string) => void,
    stop: (label?: string) => void
  ) => void,
  options?: {
    numberOfRuns: number;
    numberOfWarmups: number;
  }
): Promise<void>;

Package Sidebar

Install

npm i howslow

Weekly Downloads

615

Version

0.1.0

License

MIT

Unpacked Size

5.88 kB

Total Files

5

Last publish

Collaborators

  • mitschabaude