3.5.5 • Published 10 days ago

@thi.ng/bench v3.5.5

Weekly downloads
64
License
Apache-2.0
Repository
github
Last release
10 days ago

bench

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Benchmarking utilities w/ various statistics & formatters (CSV, Markdown etc.).

Though no public API change (only additions), since v2.0.0 this library internally attempts to use high-res ES BigInt timestamps (in Node via process.hrtime.bigint()). If BigInt is not available in the target environment, timestamps are still only sourced via Date.now().

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

Installation

yarn add @thi.ng/bench

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/bench"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const bench = await import("@thi.ng/bench");

Package sizes (gzipped, pre-treeshake): ESM: 1.39 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Doodle w/ K-nearest neighbor search result visualizationDemoSource
Poisson-disk shape-aware sampling, Voronoi & Minimum Spanning Tree visualizationDemoSource
Minimal Markdown to Hiccup to HTML parser / transformerDemoSource
Parser grammar livecoding editor/playground & codegenDemoSource
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixelDemoSource
Full umbrella repo doc string search w/ paginated resultsDemoSource
Fork-join worker-based raymarch renderer (JS/CPU only)DemoSource

API

Generated API docs

import { timed, bench, benchmark } from "@thi.ng/bench";

// test functions
const fib = (n) => n > 2 ? fib(n - 1) + fib(n - 2) : n > 0 ? 1 : 0;

const fib2 = (n) => {
    const res = [0, 1];
    for(let i = 2; i <= n; i++) {
        res[i] = res[i - 1] + res[i - 2];
    }
    return res[n];
};

// measure single execution time
timed(() => fib(40));
// 714ms
// 102334155
timed(() => fib2(40));
// 0ms
// 102334155

// measure 1mil iterations (default)
bench(() => fib(10), 1e6);
// 395ms
// 55

bench(() => fib2(10), 1e6);
// 53ms
// 55

Benchmarking with statistics

The benchmark() function executes a number of warmup runs, before executing the main measurement and producing a number of useful statistics: mean, median, min/max, 1st/3rd quartile, standard deviation (as percentage)...

See api.ts for configuration options.

Also see the formatting section below for other output options. This example uses the default format...

benchmark(() => fib(40), { title: "fib", iter: 10, warmup: 5 });
// benchmarking: fib
//         warmup... 3707.17ms (5 runs)
//         executing...
//         total: 7333.72ms, runs: 10
//         mean: 733.37ms, median: 733.79ms, range: [728.58..743.43]
//         q1: 730.98ms, q3: 735.03ms
//         sd: 0.54%

// also returns results:
// {
//   title: "fib",
//   iter: 10,
//   total: 7333.72402,
//   mean: 733.372402,
//   median: 733.794194,
//   min: 728.5808,
//   max: 743.432538,
//   q1: 730.980115,
//   q3: 735.025314,
//   sd: 0.542200865574415
// }

Benchmark suites

Multiple benchmarks can be run sequentially as suite (also returns an array of all results):

b.suite(
    [
        { title: "fib2(10)", fn: () => fib2(10) },
        { title: "fib2(20)", fn: () => fib2(20) },
        { title: "fib2(30)", fn: () => fib2(30) },
        { title: "fib2(40)", fn: () => fib2(40) },
    ],
    { iter: 10, size: 100000, warmup: 5, format: b.FORMAT_MD }
)

// |                   Title|    Iter|    Size|       Total|    Mean|  Median|     Min|     Max|      Q1|      Q3|     SD%|
// |------------------------|-------:|-------:|-----------:|-------:|-------:|-------:|-------:|-------:|-------:|-------:|
// |                fib2(10)|      10|  100000|       54.34|    5.43|    5.15|    4.40|    8.14|    4.84|    6.67|   20.32|
// |                fib2(20)|      10|  100000|      121.24|   12.12|   12.13|   11.73|   12.91|   11.93|   12.35|    2.61|
// |                fib2(30)|      10|  100000|      152.98|   15.30|   14.51|   13.93|   20.77|   14.35|   16.35|   12.65|
// |                fib2(40)|      10|  100000|      164.79|   16.48|   15.60|   15.01|   19.27|   15.42|   18.80|    9.34|

Same table as actual Markdown:

TitleIterSizeTotalMeanMedianMinMaxQ1Q3SD%
fib2(10)1010000054.345.435.154.408.144.846.6720.32
fib2(20)10100000121.2412.1212.1311.7312.9111.9312.352.61
fib2(30)10100000152.9815.3014.5113.9320.7714.3516.3512.65
fib2(40)10100000164.7916.4815.6015.0119.2715.4218.809.34

Output formatting

The following output formatters are available. Custom formatters can be easily defined (see source for examples). Formatters are configured via the format option given to benchmark() or suite().

  • FORMAT_DEFAULT - default plain text formatting
  • FORMAT_CSV - Comma-separated values (w/ column header)
  • FORMAT_MD - Markdown table format

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-bench,
  title = "@thi.ng/bench",
  author = "Karsten Schmidt",
  note = "https://thi.ng/bench",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

3.5.5

10 days ago

3.5.4

12 days ago

3.5.3

21 days ago

3.5.2

24 days ago

3.5.1

1 month ago

3.5.0

2 months ago

3.4.33

2 months ago

3.4.32

2 months ago

3.4.31

2 months ago

3.4.30

2 months ago

3.4.27

2 months ago

3.4.28

2 months ago

3.4.29

2 months ago

3.4.26

2 months ago

3.4.25

3 months ago

3.4.21

3 months ago

3.4.22

3 months ago

3.4.23

3 months ago

3.4.24

3 months ago

3.4.20

3 months ago

3.4.19

3 months ago

3.4.17

4 months ago

3.4.18

4 months ago

3.4.15

5 months ago

3.4.16

5 months ago

3.4.14

5 months ago

3.4.13

5 months ago

3.4.12

5 months ago

3.4.4

9 months ago

3.4.3

9 months ago

3.4.2

9 months ago

3.4.1

9 months ago

3.4.10

6 months ago

3.4.11

5 months ago

3.4.8

6 months ago

3.4.7

6 months ago

3.4.6

8 months ago

3.4.5

8 months ago

3.4.0

11 months ago

3.3.0

11 months ago

3.2.11

12 months ago

3.2.10

1 year ago

3.2.6

1 year ago

3.2.9

1 year ago

3.2.8

1 year ago

3.2.7

1 year ago

3.2.5

1 year ago

3.2.4

1 year ago

3.2.3

1 year ago

3.2.2

1 year ago

3.2.0

1 year ago

3.1.23

1 year ago

3.1.22

1 year ago

3.1.21

1 year ago

3.1.20

1 year ago

3.1.14

2 years ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.18

1 year ago

3.1.17

1 year ago

3.1.19

1 year ago

3.1.13

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.4

2 years ago

3.0.8

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.7

2 years ago

3.0.4

3 years ago

3.0.6

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.6

3 years ago

2.1.4

3 years ago

2.1.5

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.28

3 years ago

2.0.29

3 years ago

2.0.31

3 years ago

2.0.30

3 years ago

2.0.27

3 years ago

2.0.26

3 years ago

2.0.25

3 years ago

2.0.24

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.18

4 years ago

2.0.17

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.3.1

5 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago