Benchmark.js documentation
Benchmark
Benchmark(name, fn [, options={}])
The Benchmark constructor.
Note: The Benchmark constructor exposes a handful of lodash methods to make working with arrays, collections, and objects easier. The lodash methods are:
each/forEach, forOwn, has, indexOf, map, and reduce
Arguments
name(string): A name to identify the benchmark.fn(Function|string): The test to benchmark.[options={}](Object): Options object.
Example
// basic usage (the `new` operator is optional)
var bench = new Benchmark(fn);// or using a name first
var bench = new Benchmark('foo', fn);
// or with options
var bench = new Benchmark('foo', fn, {
// displayed by `Benchmark#toString` if `name` is not available
'id': 'xyz',
// called when the benchmark starts running
'onStart': onStart,
// called after each run cycle
'onCycle': onCycle,
// called when aborted
'onAbort': onAbort,
// called when a test errors
'onError': onError,
// called when reset
'onReset': onReset,
// called when the benchmark completes running
'onComplete': onComplete,
// compiled/called before the test loop
'setup': setup,
// compiled/called after the test loop
'teardown': teardown
});
// or name and options
var bench = new Benchmark('foo', {
// a flag to indicate the benchmark is deferred
'defer': true,
// benchmark test function
'fn': function(deferred) {
// call `Deferred#resolve` when the deferred test is finished
deferred.resolve();
}
});
// or options only
var bench = new Benchmark({
// benchmark name
'name': 'foo',
// benchmark test as a string
'fn': '[1,2,3,4].sort()'
});
// a test’s `this` binding is set to the benchmark instance
var bench = new Benchmark('foo', function() {
'My name is '.concat(this.name); // "My name is foo"
});
Benchmark.filter(array, callback)
A generic Array#filter like method.
Arguments
array(Array): The array to iterate over.callback(Function|string): The function/alias called per iteration.
Returns
(Array): A new array of values that passed callback filter.
Example
// get odd numbers
Benchmark.filter([1, 2, 3, 4, 5], function(n) {
return n % 2;
}); // -> [1, 3, 5];// get fastest benchmarks
Benchmark.filter(benches, 'fastest');
// get slowest benchmarks
Benchmark.filter(benches, 'slowest');
// get benchmarks that completed without erroring
Benchmark.filter(benches, 'successful');
Benchmark.formatNumber(number)
Converts a number to a more readable comma-separated string representation.
Arguments
number(number): The number to convert.
Returns
(string): The more readable string representation.
Benchmark.invoke(benches, name [, args])
Invokes a method on all items in an array.
Arguments
benches(Array): Array of benchmarks to iterate over.name(Object|string): The name of the method to invoke OR options object.[args](…)*: Arguments to invoke the method with.
Returns
(Array): A new array of values returned from each method invoked.
Example
// invoke `reset` on all benchmarks
Benchmark.invoke(benches, 'reset');// invoke `emit` with arguments
Benchmark.invoke(benches, 'emit', 'complete', listener);
// invoke `run(true)`, treat benchmarks as a queue, and register invoke callbacks
Benchmark.invoke(benches, {
// invoke the `run` method
'name': 'run',
// pass a single argument
'args': true,
// treat as queue, removing benchmarks from front of `benches` until empty
'queued': true,
// called before any benchmarks have been invoked.
'onStart': onStart,
// called between invoking benchmarks
'onCycle': onCycle,
// called after all benchmarks have been invoked.
'onComplete': onComplete
});
Benchmark.join(object [, separator1=',', separator2=': '])
Creates a string of joined array values or object key-value pairs.
Arguments
object(Array|Object): The object to operate on.[separator1=','](string): The separator used between key-value pairs.[separator2=': '](string): The separator used between keys and values.
Returns
(string): The joined result.
Benchmark.prototype
Benchmark.prototype.setup
(Function, string): Compiled into the test and executed immediately before the test loop.
Example
// basic usage
var bench = Benchmark({
'setup': function() {
var c = this.count,
element = document.getElementById('container');
while (c--) {
element.appendChild(document.createElement('div'));
}
},
'fn': function() {
element.removeChild(element.lastChild);
}
});// compiles to something like:
var c = this.count,
element = document.getElementById('container');
while (c--) {
element.appendChild(document.createElement('div'));
}
var start = new Date;
while (count--) {
element.removeChild(element.lastChild);
}
var end = new Date - start;
// or using strings
var bench = Benchmark({
'setup': '\
var a = 0;\n\
(function() {\n\
(function() {\n\
(function() {',
'fn': 'a += 1;',
'teardown': '\
}())\n\
}())\n\
}())'
});
// compiles to something like:
var a = 0;
(function() {
(function() {
(function() {
var start = new Date;
while (count--) {
a += 1;
}
var end = new Date - start;
}())
}())
}())
Benchmark.prototype.teardown
(Function, string): Compiled into the test and executed immediately after the test loop.
Benchmark.prototype.abort()
Aborts the benchmark without recording times.
Returns
(Object): The benchmark instance.
Benchmark.prototype.clone(options)
Creates a new benchmark using the same test and options.
Arguments
options(Object): Options object to overwrite cloned options.
Returns
(Object): The new benchmark instance.
Example
var bizarro = bench.clone({
'name': 'doppelganger'
});
Benchmark.prototype.compare(other)
Determines if a benchmark is faster than another.
Arguments
other(Object): The benchmark to compare.
Returns
(number): Returns -1 if slower, 1 if faster, and 0 if indeterminate.
Benchmark.Suite.prototype.emit(type [, args])
Executes all registered listeners of the specified event type.
Arguments
type(Object|string): The event type or object.[args](…)*: Arguments to invoke the listener with.
Returns
()*: Returns the return value of the last listener executed.
Benchmark.Suite.prototype.listeners(type)
Returns an array of event listeners for a given type that can be manipulated to add or remove listeners.
Arguments
type(string): The event type.
Returns
(Array): The listeners array.
Benchmark.Suite.prototype.off([type, listener])
Unregisters a listener for the specified event type(s), or unregisters all listeners for the specified event type(s), or unregisters all listeners for all event types.
Arguments
[type](string): The event type.[listener](Function): The function to unregister.
Returns
(Object): The benchmark instance.
Example
// unregister a listener for an event type
bench.off('cycle', listener);// unregister a listener for multiple event types
bench.off('start cycle', listener);
// unregister all listeners for an event type
bench.off('cycle');
// unregister all listeners for multiple event types
bench.off('start cycle complete');
// unregister all listeners for all event types
bench.off();
Benchmark.Suite.prototype.on(type, listener)
Registers a listener for the specified event type(s).
Arguments
type(string): The event type.listener(Function): The function to register.
Returns
(Object): The benchmark instance.
Example
// register a listener for an event type
bench.on('cycle', listener);// register a listener for multiple event types
bench.on('start cycle', listener);
Benchmark.prototype.reset()
Reset properties and abort if running.
Returns
(Object): The benchmark instance.
Benchmark.options
Benchmark.options.async
(boolean): A flag to indicate that benchmark cycles will execute asynchronously by default.
Benchmark.options.id
(string): Displayed by Benchmark#toString when a name is not available (auto-generated if absent).
Benchmark.options.initCount
(number): The default number of times to execute a test on a benchmark’s first cycle.
Benchmark.options.maxTime
(number): The maximum time a benchmark is allowed to run before finishing (secs).
Note: Cycle delays aren't counted toward the maximum time.
Benchmark.options.minSamples
(number): The minimum sample size required to perform statistical analysis.
Benchmark.platform
Benchmark.platform
(Object): Platform object with properties describing things like browser name, version, and operating system. See platform.js.
Benchmark.support
Benchmark.prototype.stats
Benchmark.prototype.stats
(Object): An object of stats including mean, margin or error, and standard deviation.
Benchmark.prototype.times
Benchmark.Deferred
Benchmark.Deferred.prototype
Benchmark.Deferred.prototype.cycles
(number): The number of deferred cycles performed while benchmarking.
Benchmark.Event
Benchmark.Event.prototype
Benchmark.Event.prototype.aborted
(boolean): A flag to indicate if the emitters listener iteration is aborted.
Benchmark.Event.prototype.cancelled
(boolean): A flag to indicate if the default action is cancelled.
Benchmark.Event.prototype.currentTarget
(Object): The object whose listeners are currently being processed.
Benchmark.Suite
Benchmark.Suite(name [, options={}])
The Suite constructor.
Note: Each Suite instance has a handful of wrapped lodash methods to make working with Suites easier. The wrapped lodash methods are:
each/forEach, indexOf, map, and reduce
Arguments
name(string): A name to identify the suite.[options={}](Object): Options object.
Example
// basic usage (the `new` operator is optional)
var suite = new Benchmark.Suite;// or using a name first
var suite = new Benchmark.Suite('foo');
// or with options
var suite = new Benchmark.Suite('foo', {
// called when the suite starts running
'onStart': onStart,
// called between running benchmarks
'onCycle': onCycle,
// called when aborted
'onAbort': onAbort,
// called when a test errors
'onError': onError,
// called when reset
'onReset': onReset,
// called when the suite completes running
'onComplete': onComplete
});
Benchmark.Suite.prototype
Benchmark.Suite.prototype.abort()
Aborts all benchmarks in the suite.
Returns
(Object): The suite instance.
Benchmark.Suite.prototype.add(name, fn [, options={}])
Adds a test to the benchmark suite.
Arguments
name(string): A name to identify the benchmark.fn(Function|string): The test to benchmark.[options={}](Object): Options object.
Returns
(Object): The benchmark instance.
Example
// basic usage
suite.add(fn);// or using a name first
suite.add('foo', fn);
// or with options
suite.add('foo', fn, {
'onCycle': onCycle,
'onComplete': onComplete
});
// or name and options
suite.add('foo', {
'fn': fn,
'onCycle': onCycle,
'onComplete': onComplete
});
// or options only
suite.add({
'name': 'foo',
'fn': fn,
'onCycle': onCycle,
'onComplete': onComplete
});
Benchmark.Suite.prototype.clone(options)
Creates a new suite with cloned benchmarks.
Arguments
options(Object): Options object to overwrite cloned options.
Returns
(Object): The new suite instance.
Benchmark.Suite.prototype.emit(type [, args])
Executes all registered listeners of the specified event type.
Arguments
type(Object|string): The event type or object.[args](…)*: Arguments to invoke the listener with.
Returns
()*: Returns the return value of the last listener executed.
Benchmark.Suite.prototype.filter(callback)
An Array#filter like method.
Arguments
callback(Function|string): The function/alias called per iteration.
Returns
(Object): A new suite of benchmarks that passed callback filter.
Benchmark.Suite.prototype.listeners(type)
Returns an array of event listeners for a given type that can be manipulated to add or remove listeners.
Arguments
type(string): The event type.
Returns
(Array): The listeners array.
Benchmark.Suite.prototype.off([type, listener])
Unregisters a listener for the specified event type(s), or unregisters all listeners for the specified event type(s), or unregisters all listeners for all event types.
Arguments
[type](string): The event type.[listener](Function): The function to unregister.
Returns
(Object): The benchmark instance.
Example
// unregister a listener for an event type
bench.off('cycle', listener);// unregister a listener for multiple event types
bench.off('start cycle', listener);
// unregister all listeners for an event type
bench.off('cycle');
// unregister all listeners for multiple event types
bench.off('start cycle complete');
// unregister all listeners for all event types
bench.off();
Benchmark.Suite.prototype.on(type, listener)
Registers a listener for the specified event type(s).
Arguments
type(string): The event type.listener(Function): The function to register.
Returns
(Object): The benchmark instance.
Example
// register a listener for an event type
bench.on('cycle', listener);// register a listener for multiple event types
bench.on('start cycle', listener);