Gathering detailed insights and metrics for tinybench
Gathering detailed insights and metrics for tinybench
Gathering detailed insights and metrics for tinybench
Gathering detailed insights and metrics for tinybench
npm install tinybench
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,899 Stars
344 Commits
37 Forks
12 Watching
6 Branches
26 Contributors
Updated on 28 Nov 2024
TypeScript (98.42%)
JavaScript (1.58%)
Cumulative downloads
Total Downloads
Last day
-10.8%
1,139,129
Compared to previous day
Last week
3.5%
6,696,651
Compared to previous week
Last month
10.6%
27,626,159
Compared to previous month
Last year
198.2%
221,222,881
Compared to previous year
No dependencies detected.
Benchmark your code easily with Tinybench, a simple, tiny and light-weight 10KB
(2KB
minified and gzipped) benchmarking library!
You can run your benchmarks in multiple JavaScript runtimes, Tinybench is completely based on the Web APIs with proper timing using
process.hrtime
or performance.now
.
Event
and EventTarget
compatible eventsIn case you need more tiny libraries like tinypool or tinyspy, please consider submitting an RFC
1$ npm install -D tinybench
You can start benchmarking by instantiating the Bench
class and adding benchmark tasks to it.
1import { Bench } from 'tinybench' 2 3const bench = new Bench({ name: 'simple benchmark', time: 100 }) 4 5bench 6 .add('faster task', () => { 7 console.log('I am faster') 8 }) 9 .add('slower task', async () => { 10 await new Promise(resolve => setTimeout(resolve, 1)) // we wait 1ms :) 11 console.log('I am slower') 12 }) 13 14await bench.run() 15 16console.log(bench.name) 17console.table(bench.table()) 18 19// Output: 20// simple benchmark 21// ┌─────────┬───────────────┬────────────────────────────┬───────────────────────────┬──────────────────────┬─────────────────────┬─────────┐ 22// │ (index) │ Task name │ Throughput average (ops/s) │ Throughput median (ops/s) │ Latency average (ns) │ Latency median (ns) │ Samples │ 23// ├─────────┼───────────────┼────────────────────────────┼───────────────────────────┼──────────────────────┼─────────────────────┼─────────┤ 24// │ 0 │ 'faster task' │ '102906 ± 0.89%' │ '82217 ± 14' │ '11909.14 ± 3.95%' │ '12163.00 ± 2.00' │ 8398 │ 25// │ 1 │ 'slower task' │ '988 ± 26.26%' │ '710' │ '1379560.47 ± 6.72%' │ '1408552.00' │ 73 │ 26// └─────────┴───────────────┴────────────────────────────┴───────────────────────────┴──────────────────────┴─────────────────────┴─────────┘
The add
method accepts a task name and a task function, so it can benchmark
it! This method returns a reference to the Bench instance, so it's possible to
use it to create an another task for that instance.
Note that the task name should always be unique in an instance, because Tinybench stores the tasks based
on their names in a Map
.
Also note that tinybench
does not log any result by default. You can extract the relevant stats
from bench.tasks
or any other API after running the benchmark, and process them however you want.
More usage examples can be found in the examples directory.
Bench
Task
TaskResult
Events
Both the Task
and Bench
classes extend the EventTarget
object. So you can attach listeners to different types of events in each class instance using the universal addEventListener
and removeEventListener
methods.
BenchEvents
1// runs on each benchmark task's cycle 2bench.addEventListener('cycle', (evt) => { 3 const task = evt.task!; 4});
TaskEvents
1// runs only on this benchmark task's cycle 2task.addEventListener('cycle', (evt) => { 3 const task = evt.task!; 4});
BenchEvent
process.hrtime
if you want more accurate results for nodejs with process.hrtime
, then import
the hrtimeNow
function from the library and pass it to the Bench
options.
1import { hrtimeNow } from 'tinybench'
It may make your benchmarks slower.
mode
is set to null
(default), concurrency is disabled.mode
is set to 'task', each task's iterations (calls of a task function) run concurrently.mode
is set to 'bench', different tasks within the bench run concurrently. Concurrent cycles.1bench.threshold = 10 // The maximum number of concurrent tasks to run. Defaults to Number.POSITIVE_INFINITY. 2bench.concurrency = 'task' // The concurrency mode to determine how tasks are run. 3await bench.run()
Mohammad Bagher |
---|
Uzlopak | poyoho |
---|
Feel free to create issues/discussions and then PRs for the project!
Your sponsorship can make a huge difference in continuing our work in open source!
No vulnerabilities found.
No security vulnerabilities found.