Skip to content

Instantly share code, notes, and snippets.

@alsotang
Created January 16, 2014 09:55
Show Gist options
  • Save alsotang/8452415 to your computer and use it in GitHub Desktop.
Save alsotang/8452415 to your computer and use it in GitHub Desktop.
The run time of the three tests are almost the same.
var should = require('should');
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
var lambdaLifting = function () {
var x = 10;
function fn() {
return x + 1;
}
return fn();
};
var change = function () {
var x = 10;
function fn() {
x += 1;
return x;
}
return fn();
};
var fn = function (x) {
return x + 1;
};
var normal = function () {
var x = 10;
return fn(x);
};
suite
.add('lambda lifting', function () {
var num = lambdaLifting();
num.should.equal(11);
})
.add('change state', function () {
var num = change();
num.should.equal(11);
})
.add('normal', function () {
var num = normal();
num.should.equal(11);
})
.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 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment