Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Last active October 13, 2015 09:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom32i/821cbd861ba2beafd0f6 to your computer and use it in GitHub Desktop.
Save Tom32i/821cbd861ba2beafd0f6 to your computer and use it in GitHub Desktop.
A node.js + browser compatible benchmark suite

Benchmark in node.js & browser

Install

npm install 

Usage:

Node:

node benchmark.js

Browser:

Navigate to index.html and open console.

if (typeof(require) !== 'undefined') {
var Benchmark = require('benchmark');
}
var suite = new Benchmark.Suite('My benchmark');
// Functions to compare
suite
.add('function#A', function() {
// Version A
})
.add('function#B', function() {
// Version B
})
// Listeners
.on('start', function() {
console.log('Started "%s"', this.name);
if (typeof(document) !== 'undefined') {
document.title = this.name;
}
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// Run
.run(/*{ 'async': true }*/);
<!DOCTYPE html>
<html>
<head>
<title>Benchmark</title>
</head>
<body>
<script src="node_modules/benchmark/benchmark.js"></script>
<script src="benchmark.js"></script>
</body>
</html>
{
"name": "benchmark",
"version": "1.0.0",
"description": "A node.js + browser compatible benchmark suite",
"main": "benchmark.js",
"author": "Thomas Jarrand",
"license": "MIT",
"dependencies": {
"benchmark": "^1.0.0",
"microtime": "^2.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment