You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.6 KiB

  1. # Benchmark.js <sup>v1.0.0</sup>
  2. 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/).
  3. ## BestieJS
  4. 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.
  5. ## Documentation
  6. The documentation for Benchmark.js can be viewed here: <http://benchmarkjs.com/docs>
  7. For a list of upcoming features, check out our [roadmap](https://github.com/bestiejs/benchmark.js/wiki/Roadmap).
  8. ## Support
  9. 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.
  10. ## Installation and usage
  11. In a browser or Adobe AIR:
  12. ~~~ html
  13. <script src="benchmark.js"></script>
  14. ~~~
  15. Optionally, expose Java’s nanosecond timer by adding the `nano` applet to the `<body>`:
  16. ~~~ html
  17. <applet code="nano" archive="nano.jar"></applet>
  18. ~~~
  19. Or enable Chrome’s microsecond timer by using the [command line switch](http://peter.sh/experiments/chromium-command-line-switches/#enable-benchmarking):
  20. --enable-benchmarking
  21. Via [npm](http://npmjs.org/):
  22. ~~~ bash
  23. npm install benchmark
  24. ~~~
  25. In [Node.js](http://nodejs.org/) and [RingoJS v0.8.0+](http://ringojs.org/):
  26. ~~~ js
  27. var Benchmark = require('benchmark');
  28. ~~~
  29. Optionally, use the [microtime module](https://github.com/wadey/node-microtime) by Wade Simmons:
  30. ~~~ bash
  31. npm install microtime
  32. ~~~
  33. In [RingoJS v0.7.0-](http://ringojs.org/):
  34. ~~~ js
  35. var Benchmark = require('benchmark').Benchmark;
  36. ~~~
  37. In [Rhino](http://www.mozilla.org/rhino/):
  38. ~~~ js
  39. load('benchmark.js');
  40. ~~~
  41. In an AMD loader like [RequireJS](http://requirejs.org/):
  42. ~~~ js
  43. require({
  44. 'paths': {
  45. 'benchmark': 'path/to/benchmark'
  46. }
  47. },
  48. ['benchmark'], function(Benchmark) {
  49. console.log(Benchmark.version);
  50. });
  51. // or with platform.js
  52. // https://github.com/bestiejs/platform.js
  53. require({
  54. 'paths': {
  55. 'benchmark': 'path/to/benchmark',
  56. 'platform': 'path/to/platform'
  57. }
  58. },
  59. ['benchmark', 'platform'], function(Benchmark, platform) {
  60. Benchmark.platform = platform;
  61. console.log(Benchmark.platform.name);
  62. });
  63. ~~~
  64. Usage example:
  65. ~~~ js
  66. var suite = new Benchmark.Suite;
  67. // add tests
  68. suite.add('RegExp#test', function() {
  69. /o/.test('Hello World!');
  70. })
  71. .add('String#indexOf', function() {
  72. 'Hello World!'.indexOf('o') > -1;
  73. })
  74. // add listeners
  75. .on('cycle', function(event) {
  76. console.log(String(event.target));
  77. })
  78. .on('complete', function() {
  79. console.log('Fastest is ' + this.filter('fastest').pluck('name'));
  80. })
  81. // run async
  82. .run({ 'async': true });
  83. // logs:
  84. // > RegExp#test x 4,161,532 +-0.99% (59 cycles)
  85. // > String#indexOf x 6,139,623 +-1.00% (131 cycles)
  86. // > Fastest is String#indexOf
  87. ~~~
  88. ## Authors
  89. * [Mathias Bynens](http://mathiasbynens.be/)
  90. [![twitter/mathias](http://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter")
  91. * [John-David Dalton](http://allyoucanleet.com/)
  92. [![twitter/jdalton](http://gravatar.com/avatar/299a3d891ff1920b69c364d061007043?s=70)](https://twitter.com/jdalton "Follow @jdalton on Twitter")
  93. ## Contributors
  94. * [Kit Cambridge](http://kitcambridge.github.com/)
  95. [![twitter/kitcambridge](http://gravatar.com/avatar/6662a1d02f351b5ef2f8b4d815804661?s=70)](https://twitter.com/kitcambridge "Follow @kitcambridge on Twitter")