Benchmark.js v2.0.0-pre
Benchmark
Benchmark.version
Benchmark.filter
Benchmark.formatNumber
Benchmark.invoke
Benchmark.join
Benchmark.runInContext
Benchmark.prototype.aborted
Benchmark.prototype.compiled
Benchmark.prototype.count
Benchmark.prototype.cycles
Benchmark.prototype.error
Benchmark.prototype.fn
Benchmark.prototype.hz
Benchmark.prototype.running
Benchmark.prototype.setup
Benchmark.prototype.teardown
Benchmark.prototype.abort
Benchmark.prototype.clone
Benchmark.prototype.compare
Benchmark.prototype.emit
Benchmark.prototype.listeners
Benchmark.prototype.off
Benchmark.prototype.on
Benchmark.prototype.reset
Benchmark.prototype.run
Benchmark.prototype.toString
Benchmark.options
Benchmark.options.async
Benchmark.options.defer
Benchmark.options.delay
Benchmark.options.id
Benchmark.options.initCount
Benchmark.options.maxTime
Benchmark.options.minSamples
Benchmark.options.minTime
Benchmark.options.name
Benchmark.options.onAbort
Benchmark.options.onComplete
Benchmark.options.onCycle
Benchmark.options.onError
Benchmark.options.onReset
Benchmark.options.onStart
Benchmark.support
Benchmark.support.browser
Benchmark.support.decompilation
Benchmark.support.java
Benchmark.support.timeout
Benchmark.support.unshiftResult
Benchmark.prototype.stats
Benchmark.prototype.stats.deviation
Benchmark.prototype.stats.mean
Benchmark.prototype.stats.moe
Benchmark.prototype.stats.rme
Benchmark.prototype.stats.sample
Benchmark.prototype.stats.sem
Benchmark.prototype.stats.variance
Benchmark.prototype.times
Benchmark.prototype.times.cycle
Benchmark.prototype.times.elapsed
Benchmark.prototype.times.period
Benchmark.prototype.times.timeStamp
Benchmark.Deferred.prototype.benchmark
Benchmark.Deferred.prototype.cycles
Benchmark.Deferred.prototype.elapsed
Benchmark.Deferred.prototype.timeStamp
Benchmark.Event.prototype.aborted
Benchmark.Event.prototype.cancelled
Benchmark.Event.prototype.currentTarget
Benchmark.Event.prototype.result
Benchmark.Event.prototype.target
Benchmark.Event.prototype.timeStamp
Benchmark.Event.prototype.type
Benchmark.Suite.prototype.aborted
Benchmark.Suite.prototype.length
Benchmark.Suite.prototype.running
Benchmark.Suite.prototype.abort
Benchmark.Suite.prototype.add
Benchmark.Suite.prototype.clone
Benchmark.Suite.prototype.emit
Benchmark.Suite.prototype.filter
Benchmark.Suite.prototype.listeners
Benchmark.Suite.prototype.off
Benchmark.Suite.prototype.on
Benchmark.Suite.prototype.reset
Benchmark.Suite.prototype.run
The Benchmark constructor.
Note: The Benchmark constructor exposes a handful of Lo-Dash methods to make working with arrays, collections, and objects easier. The Lo-Dash methods are:
[each/forEach
](https://lodash.com/docs#forEach), [forOwn
](https://lodash.com/docs#forOwn), [has
](https://lodash.com/docs#has), [indexOf
](https://lodash.com/docs#indexOf), [map
](https://lodash.com/docs#map), [pluck
](https://lodash.com/docs#pluck), and [reduce
](https://lodash.com/docs#reduce)
name
(string): A name to identify the benchmark.fn
(Function|string): The test to benchmark.[options={}]
(Object): Options object.
// 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"
});
(string): The semantic version number.
A generic Array#filter
like method.
array
(Array): The array to iterate over.callback
(Function|string): The function/alias called per iteration.thisArg
(*): Thethis
binding for the callback.
(Array): A new array of values that passed callback filter.
// 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');
Converts a number to a more readable comma-separated string representation.
number
(number): The number to convert.
(string): The more readable string representation.
Invokes a method on all items in an array.
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.
(Array): A new array of values returned from each method invoked.
// 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
});
Creates a string of joined array values or object key-value pairs.
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.
(string): The joined result.
Create a new Benchmark
function using the given context
object.
[context=root]
(Object): The context object.
(Function): Returns a new Benchmark
function.
(boolean): A flag to indicate if the benchmark is aborted.
(Function, string): The compiled test function.
(number): The number of times a test was executed.
(number): The number of cycles performed while benchmarking.
(Object): The error object if the test failed.
(Function, string): The test to benchmark.
(number): The number of executions per second.
(boolean): A flag to indicate if the benchmark is running.
(Function, string): Compiled into the test and executed immediately **before** the test loop.
// 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;
}())
}())
}())
(Function, string): Compiled into the test and executed immediately **after** the test loop.
Aborts the benchmark without recording times.
(Object): The benchmark instance.
Creates a new benchmark using the same test and options.
options
(Object): Options object to overwrite cloned options.
(Object): The new benchmark instance.
var bizarro = bench.clone({
'name': 'doppelganger'
});
Determines if a benchmark is faster than another.
other
(Object): The benchmark to compare.
(number): Returns -1
if slower, 1
if faster, and 0
if indeterminate.
Executes all registered listeners of the specified event type.
type
(Object|string): The event type or object.[args]
(...*): Arguments to invoke the listener with.
(*): Returns the return value of the last listener executed.
Returns an array of event listeners for a given type that can be manipulated to add or remove listeners.
type
(string): The event type.
(Array): The listeners array.
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.
[type]
(string): The event type.[listener]
(Function): The function to unregister.
(Object): The benchmark instance.
// 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();
Registers a listener for the specified event type(s).
type
(string): The event type.listener
(Function): The function to register.
(Object): The benchmark instance.
// register a listener for an event type
bench.on('cycle', listener);
// register a listener for multiple event types
bench.on('start cycle', listener);
Reset properties and abort if running.
(Object): The benchmark instance.
Runs the benchmark.
[options={}]
(Object): Options object.
(Object): The benchmark instance.
// basic usage
bench.run();
// or with options
bench.run({ 'async': true });
Displays relevant benchmark information when coerced to a string.
(string): A string representation of the benchmark instance.
(Object): The default options copied by benchmark instances.
(boolean): A flag to indicate that benchmark cycles will execute asynchronously by default.
(boolean): A flag to indicate that the benchmark clock is deferred.
(number): The delay between test cycles (secs).
(string): Displayed by Benchmark#toString
when a name
is not available (auto-generated if absent).
(number): The default number of times to execute a test on a benchmark's first cycle.
(number): The maximum time a benchmark is allowed to run before finishing (secs).
Note: Cycle delays aren't counted toward the maximum time.
(number): The minimum sample size required to perform statistical analysis.
(number): The time needed to reduce the percent uncertainty of measurement to 1
% (secs).
(string): The name of the benchmark.
An event listener called when the benchmark is aborted.
An event listener called when the benchmark completes running.
An event listener called after each run cycle.
An event listener called when a test errors.
An event listener called when the benchmark is reset.
An event listener called when the benchmark starts running.
(Object): Platform object with properties describing things like browser name, version, and operating system. See [platform.js
](http://mths.be/platform).
(Object): An object used to flag environments/features.
(boolean): Detect if running in a browser environment.
(boolean): Detect if function decompilation is support.
(boolean): Detect if Java is enabled/exposed.
(boolean): Detect if the Timers API exists.
(boolean): Detect if Array#unshift
returns the new length of the array (all but IE < 8
).
(Object): An object of stats including mean, margin or error, and standard deviation.
(number): The sample standard deviation.
(number): The sample arithmetic mean (secs).
(number): The margin of error.
(number): The relative margin of error (expressed as a percentage of the mean).
(Array): The array of sampled periods.
(number): The standard error of the mean.
(number): The sample variance.
(Object): An object of timing data including cycle, elapsed, period, start, and stop.
(number): The time taken to complete the last cycle (secs).
(number): The time taken to complete the benchmark (secs).
(number): The time taken to execute the test once (secs).
(number): A timestamp of when the benchmark started (ms).
The Deferred constructor.
clone
(Object): The cloned benchmark instance.
(Object): The deferred benchmark instance.
(number): The number of deferred cycles performed while benchmarking.
(number): The time taken to complete the deferred benchmark (secs).
(number): A timestamp of when the deferred benchmark started (ms).
The Event constructor.
type
(Object|string): The event type.
(boolean): A flag to indicate if the emitters listener iteration is aborted.
(boolean): A flag to indicate if the default action is cancelled.
(Object): The object whose listeners are currently being processed.
(Mixed): The return value of the last executed listener.
(Object): The object to which the event was originally emitted.
(number): A timestamp of when the event was created (ms).
(string): The event type.
The Suite constructor.
Note: Each Suite instance has a handful of wrapped Lo-Dash methods to make working with Suites easier. The wrapped Lo-Dash methods are:
[each/forEach
](https://lodash.com/docs#forEach), [indexOf
](https://lodash.com/docs#indexOf), [map
](https://lodash.com/docs#map), [pluck
](https://lodash.com/docs#pluck), and [reduce
](https://lodash.com/docs#reduce)
name
(string): A name to identify the suite.[options={}]
(Object): Options object.
// 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
});
(boolean): A flag to indicate if the suite is aborted.
(number): The number of benchmarks in the suite.
(boolean): A flag to indicate if the suite is running.
Aborts all benchmarks in the suite.
(Object): The suite instance.
Adds a test to the benchmark suite.
name
(string): A name to identify the benchmark.fn
(Function|string): The test to benchmark.[options={}]
(Object): Options object.
(Object): The benchmark instance.
// 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
});
Creates a new suite with cloned benchmarks.
options
(Object): Options object to overwrite cloned options.
(Object): The new suite instance.
Executes all registered listeners of the specified event type.
type
(Object|string): The event type or object.[args]
(...*): Arguments to invoke the listener with.
(*): Returns the return value of the last listener executed.
An Array#filter
like method.
callback
(Function|string): The function/alias called per iteration.
(Object): A new suite of benchmarks that passed callback filter.
Returns an array of event listeners for a given type that can be manipulated to add or remove listeners.
type
(string): The event type.
(Array): The listeners array.
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.
[type]
(string): The event type.[listener]
(Function): The function to unregister.
(Object): The benchmark instance.
// 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();
Registers a listener for the specified event type(s).
type
(string): The event type.listener
(Function): The function to register.
(Object): The benchmark instance.
// register a listener for an event type
bench.on('cycle', listener);
// register a listener for multiple event types
bench.on('start cycle', listener);
Resets all benchmarks in the suite.
(Object): The suite instance.
Runs the suite.
[options={}]
(Object): Options object.
(Object): The suite instance.
// basic usage
suite.run();
// or with options
suite.run({ 'async': true, 'queued': true });
(Object): The default options copied by suite instances.
(string): The name of the suite.