mini-bench
A tiny asynchronous JavaScript benchmarking library
mini-bench provides a simple harness for measuring the execution time of JavaScript code. Design your experiments, analyze the numbers and present the data as you see fit.
Features:
- small (< 200 LOC)
- asynchronous, evented operation
- fixed as well as adaptive test cycles used in benchmark
- easy to use
Note: mini-bench is an up-to-date fork of uubench.
Synopsis
Set up a suite:
var Bench = ; var suite = { console; } { console; } { console; } { console; } ;
Add some benchmarks:
suite; suite;
Run the suite:
suite;
Installation
Via npm:
$ npm install mini-bench
In Node:
var Bench = require('mini-bench');
In the browser (exposed as MiniBench
):
<script src="mini-bench.js"></script>
Guide
By design, mini-bench doesn't come with extras. Instead, you use the low-level API to build your own unique benchmark suites.
Defaults
mini-bench ships with the following defaults that apply to every test suite:
Benchdefaults = type: "adaptive" // adaptive or fixed iterations: 10 // starting iterations minTime: 100 // minimum run time (ms) - adaptive only delay: 100 // delay between tests (ms) async: true // run benches in async mode (all at once)
You may override these globally or per-suite. Read on to find out what each option does.
Fixed test cycles
By default mini-bench uses adaptive test cycles to allow reasonable execution time across different environments. To use fixed cycles instead, set the type
to "fixed":
var suite = type: "fixed" iterations: 1000 // run each benchmark exactly 1000 times ... ;
Setting the minimum runtime
mini-bench defaults to a minimum run time of 100ms in adaptive mode. To adjust this run time:
var suite = minTime: 1000 // each benchmark should run for at least 1000ms ... ;
Starting iterations
In adaptive mode it is sometimes useful to bump up the starting iterations to reach the minimum runtime faster:
var suite = iterations: 1000 // run each benchmark a minimum of 1000 times ... ;
Setting the benchmark delay
mini-bench imposes a 100ms delay between benchmarks to give any UI elements that might be present time to update. This delay can be tweaked:
var suite = delay: 500 // 500ms delay between benchmarks ... ;
Disabling auto-looping
To manually loop within a given benchmark, add a second argument to the benchmark's argument list. mini-bench will then automatically disable auto-looping:
suite;
Multiple runs
To collect benchmark data over multiple runs, simply rerun the suite on completion:
var suite = ... { if --runCounter console; else suite; } ;
Beware of relying on multiple in-process runs to establish statistical relevance. Better data can be obtained by completely re-running your test scripts.
Running in sync mode
A suite may have multiple benchmarks. To run benchmarks one-at-a-time in the order they were added, set async
option to false.
var suite = async: false // run benches in sync mode (one at a time in order) ... ;
Section markers
Longer suites that have multiple benches may use the section()
method. A section is run in order (when sync option is true) and can be used to visually group benches and optionally modify parameters.
suite ; suite ;
A section emits a "section" event.
Chaining
As of v0.0.2 bench()
and section()
are chainable. This allows for easier grouping and enabling/disabling of groups.
suite ... suite ... suite;
or
suite;
Stats
Rather than imposing a limited statistical model on benchmark data, mini-bench gives you the raw numbers. If you want to go nuts with the math have a look at this gist.
Loop calibration
In most cases auto looping doesn't add enough overhead to benchmark times to be worth worrying about, but extremely fast benchmarks can suffer. Add a calibration test if you want to correct for this overhead:
suite;
You can then subtract the elapsed time of the "calibrate" test from other tests in the suite.
Examples
- Dust browser benchmarks: https://github.com/akdubya/dustjs/blob/master/benchmark/index.html
- Dust node benchmarks: http://github.com/akdubya/dustjs/blob/master/benchmark/server.js
Change log
v1.0 (Breaking change)
min
option changed tominTime
sync
option changed toasync
(default: true)- added UMD module loader
- renamed and published to npm as mini-bench
v0.0.2 (start of moos fork)
- added
sync
option to run tests in sync mode (default: false) - added
section
method to group similar tests, fires "section" event. - added chaining
About
mini-bench is a fork of uubench.
uubench was inspired by the venerable jslitmus
License
MIT