Skip to content

Instantly share code, notes, and snippets.

@yuanqing
Created June 13, 2016 15:28
Show Gist options
  • Save yuanqing/e2e382e3d38bfd4e2be28d834b8cad11 to your computer and use it in GitHub Desktop.
Save yuanqing/e2e382e3d38bfd4e2be28d834b8cad11 to your computer and use it in GitHub Desktop.
$ node index.js
ternary, true x 84,415,848 ops/sec ±2.47% (78 runs sampled)
ternary, false x 73,646,719 ops/sec ±5.86% (73 runs sampled)
&& and ||, true x 78,748,665 ops/sec ±2.80% (72 runs sampled)
&& and ||, false x 80,837,863 ops/sec ±1.84% (81 runs sampled)
Fastest is ternary, true
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var foo = true;
var bar = false;
suite.add('ternary, true', function() {
foo ? 1 : 2;
}).add('ternary, false', function() {
bar ? 1 : 2;
}).add('&& and ||, true', function() {
foo && 1 || 2;
}).add('&& and ||, false', function() {
bar && 1 || 2;
}).on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).run({ async: true });
{
"dependencies": {
"benchmark": "^2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment