1 # Benchmark.js <sup>v1.0.0</sup>
3 A [robust](http://calendar.perfplanet.com/2010/bulletproof-javascript-benchmarks/ "Bulletproof JavaScript benchmarks") benchmarking library that works on nearly all JavaScript platforms<sup><a name="fnref1" href="#fn1">1</a></sup>, supports high-resolution timers, and returns statistically significant results. As seen on [jsPerf](http://jsperf.com/).
5 ## BestieJS
7 Benchmark.js is part of the BestieJS *"Best in Class"* module collection. This means we promote solid browser/environment support, ES5 precedents, unit testing, and plenty of documentation.
9 ## Documentation
11 The documentation for Benchmark.js can be viewed here: <http://benchmarkjs.com/docs>
13 For a list of upcoming features, check out our [roadmap](https://github.com/bestiejs/benchmark.js/wiki/Roadmap).
15 ## Support
17 Benchmark.js has been tested in at least Adobe AIR 3.1, Chrome 5-21, Firefox 1.5-13, IE 6-9, Opera 9.25-12.01, Safari 3-6, Node.js 0.8.6, Narwhal 0.3.2, RingoJS 0.8, and Rhino 1.7RC5.
19 ## Installation and usage
21 In a browser or Adobe AIR:
23 ~~~ html
24 <script src="benchmark.js"></script>
25 ~~~
27 Optionally, expose Java’s nanosecond timer by adding the `nano` applet to the `<body>`:
29 ~~~ html
30 <applet code="nano" archive="nano.jar"></applet>
31 ~~~
33 Or enable Chrome’s microsecond timer by using the [command line switch](http://peter.sh/experiments/chromium-command-line-switches/#enable-benchmarking):
35 --enable-benchmarking
37 Via [npm](http://npmjs.org/):
39 ~~~ bash
40 npm install benchmark
41 ~~~
43 In [Node.js](http://nodejs.org/) and [RingoJS v0.8.0+](http://ringojs.org/):
45 ~~~ js
46 var Benchmark = require('benchmark');
47 ~~~
49 Optionally, use the [microtime module](https://github.com/wadey/node-microtime) by Wade Simmons:
51 ~~~ bash
52 npm install microtime
53 ~~~
55 In [RingoJS v0.7.0-](http://ringojs.org/):
57 ~~~ js
58 var Benchmark = require('benchmark').Benchmark;
59 ~~~
61 In [Rhino](http://www.mozilla.org/rhino/):
63 ~~~ js
64 load('benchmark.js');
65 ~~~
67 In an AMD loader like [RequireJS](http://requirejs.org/):
69 ~~~ js
70 require({
71 'paths': {
72 'benchmark': 'path/to/benchmark'
73 }
74 },
75 ['benchmark'], function(Benchmark) {
76 console.log(Benchmark.version);
77 });
79 // or with platform.js
80 // https://github.com/bestiejs/platform.js
81 require({
82 'paths': {
83 'benchmark': 'path/to/benchmark',
84 'platform': 'path/to/platform'
85 }
86 },
87 ['benchmark', 'platform'], function(Benchmark, platform) {
88 Benchmark.platform = platform;
89 console.log(Benchmark.platform.name);
90 });
91 ~~~
93 Usage example:
95 ~~~ js
96 var suite = new Benchmark.Suite;
98 // add tests
99 suite.add('RegExp#test', function() {
100 /o/.test('Hello World!');
101 })
102 .add('String#indexOf', function() {
103 'Hello World!'.indexOf('o') > -1;
104 })
105 // add listeners
106 .on('cycle', function(event) {
107 console.log(String(event.target));
108 })
109 .on('complete', function() {
110 console.log('Fastest is ' + this.filter('fastest').pluck('name'));
111 })
112 // run async
113 .run({ 'async': true });
115 // logs:
116 // > RegExp#test x 4,161,532 +-0.99% (59 cycles)
117 // > String#indexOf x 6,139,623 +-1.00% (131 cycles)
118 // > Fastest is String#indexOf
119 ~~~
121 ## Authors
123 * [Mathias Bynens](http://mathiasbynens.be/)
124 [![twitter/mathias](http://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter")
125 * [John-David Dalton](http://allyoucanleet.com/)
126 [![twitter/jdalton](http://gravatar.com/avatar/299a3d891ff1920b69c364d061007043?s=70)](https://twitter.com/jdalton "Follow @jdalton on Twitter")
128 ## Contributors
130 * [Kit Cambridge](http://kitcambridge.github.com/)
131 [![twitter/kitcambridge](http://gravatar.com/avatar/6662a1d02f351b5ef2f8b4d815804661?s=70)](https://twitter.com/kitcambridge "Follow @kitcambridge on Twitter")