karma-bench
A Karma plugin to run Benchmark.js v2
Based on karma-benchmark plugin, but it don't break tests functions context
Installation
npm install karma-bench --save-dev
Karma Configuration
Reporting results on the command line
To see jsPerf style results on the command line, install karma-benchmark-reporter
:
npm install karma-benchmark-reporter --save-dev
Then, in karma.conf.js, add benchmark
to the list of reporters:
module { config;};
Run Karma:
karma start
Then, you'll then see output that looks like:
Chrome 51.0.2704 (Mac OS X 10.11.5) Array iteration: util.each at 19356910 ops/sec
Chrome 51.0.2704 (Mac OS X 10.11.5) Array iteration: Array.forEach at 2567531 ops/sec
Chrome 51.0.2704 (Mac OS X 10.11.5) Array search: util.contains at 12635982 ops/sec
Chrome 51.0.2704 (Mac OS X 10.11.5) Array search: Array.indexOf at 5828437 ops/sec
Chrome 51.0.2704 (Mac OS X 10.11.5)
Array iteration: util.each at 19356910 ops/sec (7.54x faster than Array.forEach)
Array search: util.contains at 12635982 ops/sec (2.17x faster than Array.indexOf)
Timeouts
As large suites of Benchmarks take a long time to run, you may need to increase Karma's timeout from it's default of 60000.
captureTimeout: 60000
Writing Benchmarks
Suites and benchmarks are defined using a wrapper for Benchmark.js in the form of the suite
and benchmark
globals.
Typical
In this example, a suite is defined that pits _.each
against the native Array.forEach
method:
;
Suite options
Suite options are the same as in Benchmark.js.
See the Benchmark.js Suite constructor API docs for a full list of options.
;
Benchmark options
Benchmark options are the same as in Benchmark.js.
See the Benchmark.js Benchmark constructor API docs for a full list of options.