Skip to content

Simple benchmarking utility for JavaScript, based on Ruby's Benchmark module.

Notifications You must be signed in to change notification settings

mnoble/Benchmark.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Benchmark.js

Benchmark.js is a small utility to run JavaScript benchmarks. It’s based on Ruby’s Benchmark module. A lot of the stuff below isn’t completely implemented and/or doesn’t work at all.

Install

Copy all the files from lib/ into public/javascript/benchmark' or where ever your JavaScript files go.

Usage

Benchmark.js has one main method Benchmark.benchmark() aliased to Benchmark.bm(); I told you it’s based on Ruby’s Benchmark :)

Specifying STDOUT

If you want to use a custom console or output object you can specify it with stdout. This is useful when testing, using a custom console like Blackbird or if you want to send the results somewhere.

Benchmark.initialize({ stdout: log })

where log is the output object. The output object must implement the following methods

log   => General purpose debugging or logging
warn  => For warnings
error => When throwing an error

Single Measurement

Benchmark.benchmark(function() {
  for (i = 0; i < 10000000; i++) { }
});

Outputs:

(  7.644)

Single Measurement with Label

Benchmark.benchmark("Iterate 10,000,000 times", function() {
  for (i = 0; i < 10000000; i++) { }
});

Outputs:

Iterate 10,000,000 times: (   7.149000)

Single measurement ‘n` number of times

Benchmark.benchmark(1000000, function() {
  var foo = "something";
});

Outputs:

(   7.149)

Single measurement ‘n` number of times with a label

Benchmark.benchmark('One million times', 1000000, function() {
  var foo = "something";
});

Outputs:

One million times: (   7.149)

Measure Things Sequentially

group = { toSource : function() { Object.toSource(); },
          toString : function() { 123456.toString(); }}
Benchmark.benchmark(group)

Outputs:

-----------------------
toSource: (   0.123000)
toString: (   1.712000)
------- total: 1.835000

In this case the name of each function becomes the label for that measurement. You can add spaces to the name with underscores: test_something_awesome becomes "test something awesome". To intentially use an underscore, use two: i__love__underscores becomes "i_love_underscores".

License

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

Copyright (C) 2009 Matte Noble
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

About

Simple benchmarking utility for JavaScript, based on Ruby's Benchmark module.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published