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.
Copy all the files from lib/ into public/javascript/benchmark' or where ever your JavaScript files go.
Benchmark.js has one main method Benchmark.benchmark() aliased to Benchmark.bm(); I told you it’s based on Ruby’s Benchmark :)
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
Benchmark.benchmark(function() {
for (i = 0; i < 10000000; i++) { }
});
Outputs:
( 7.644)
Benchmark.benchmark("Iterate 10,000,000 times", function() {
for (i = 0; i < 10000000; i++) { }
});
Outputs:
Iterate 10,000,000 times: ( 7.149000)
Benchmark.benchmark(1000000, function() { var foo = "something"; });
Outputs:
( 7.149)
Benchmark.benchmark('One million times', 1000000, function() { var foo = "something"; });
Outputs:
One million times: ( 7.149)
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".
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.