1 | # Benchmark.js v2.1.4
|
2 |
|
3 | A [robust](https://mathiasbynens.be/notes/javascript-benchmarking "Bulletproof JavaScript benchmarks") benchmarking library that supports high-resolution timers & returns statistically significant results. As seen on [jsPerf](https://jsperf.com/).
|
4 |
|
5 | ## Documentation
|
6 |
|
7 | * [API Documentation](https://benchmarkjs.com/docs)
|
8 |
|
9 | ## Download
|
10 |
|
11 | * [Development source](https://raw.githubusercontent.com/bestiejs/benchmark.js/2.1.4/benchmark.js)
|
12 |
|
13 | ## Installation
|
14 |
|
15 | Benchmark.js’ only hard dependency is [lodash](https://lodash.com/).
|
16 | Include [platform.js](https://mths.be/platform) to populate [Benchmark.platform](https://benchmarkjs.com/docs#platform).
|
17 |
|
18 | In a browser:
|
19 |
|
20 | ```html
|
21 | <script src="lodash.js"></script>
|
22 | <script src="platform.js"></script>
|
23 | <script src="benchmark.js"></script>
|
24 | ```
|
25 |
|
26 | In an AMD loader:
|
27 |
|
28 | ```js
|
29 | require({
|
30 | 'paths': {
|
31 | 'benchmark': 'path/to/benchmark',
|
32 | 'lodash': 'path/to/lodash',
|
33 | 'platform': 'path/to/platform'
|
34 | }
|
35 | },
|
36 | ['benchmark'], function(Benchmark) {/*…*/});
|
37 | ```
|
38 |
|
39 | Using npm:
|
40 |
|
41 | ```shell
|
42 | $ npm i --save benchmark
|
43 | ```
|
44 |
|
45 | In Node.js:
|
46 |
|
47 | ```js
|
48 | var Benchmark = require('benchmark');
|
49 | ```
|
50 |
|
51 | Optionally, use the [microtime module](https://github.com/wadey/node-microtime) by Wade Simmons:
|
52 |
|
53 | ```shell
|
54 | npm i --save microtime
|
55 | ```
|
56 |
|
57 | Usage example:
|
58 |
|
59 | ```js
|
60 | var suite = new Benchmark.Suite;
|
61 |
|
62 | // add tests
|
63 | suite.add('RegExp#test', function() {
|
64 | /o/.test('Hello World!');
|
65 | })
|
66 | .add('String#indexOf', function() {
|
67 | 'Hello World!'.indexOf('o') > -1;
|
68 | })
|
69 | // add listeners
|
70 | .on('cycle', function(event) {
|
71 | console.log(String(event.target));
|
72 | })
|
73 | .on('complete', function() {
|
74 | console.log('Fastest is ' + this.filter('fastest').map('name'));
|
75 | })
|
76 | // run async
|
77 | .run({ 'async': true });
|
78 |
|
79 | // logs:
|
80 | // => RegExp#test x 4,161,532 +-0.99% (59 cycles)
|
81 | // => String#indexOf x 6,139,623 +-1.00% (131 cycles)
|
82 | // => Fastest is String#indexOf
|
83 | ```
|
84 |
|
85 | ## Support
|
86 |
|
87 | Tested in Chrome 54-55, Firefox 49-50, IE 11, Edge 14, Safari 9-10, Node.js 6-7, & PhantomJS 2.1.1.
|
88 |
|
89 | ## BestieJS
|
90 |
|
91 | Benchmark.js is part of the BestieJS *“Best in Class”* module collection. This means we promote solid browser/environment support, ES5+ precedents, unit testing, & plenty of documentation.
|