Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Last active October 13, 2015 09:57
Show Gist options
  • Select an option

  • Save Tom32i/821cbd861ba2beafd0f6 to your computer and use it in GitHub Desktop.

Select an option

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;
suite
.add('function#A', function() {
return Math.sqrt(Math.pow(point.x1 - point.x2, 2) + Math.pow(point.y1 - point.y2, 2));
})
.add('function#B', function() {
var x = point.x1 - point.x2, y = point.y1 - point.y2;
return Math.sqrt(x*x + y*y);
})
// add listeners
.on('start', function() {
console.log('Started');
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run({ 'async': true });
<!DOCTYPE html>
<html>
<head>
<title>Benchmark</title>
</head>
<body>
<script src="node_modules/benchmark/benchmark.js"></script>
<script src="index.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