Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Created January 21, 2010 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncanbeevers/282944 to your computer and use it in GitHub Desktop.
Save duncanbeevers/282944 to your computer and use it in GitHub Desktop.
Compare = function(fn1, fn2, i) {
var time = function(fn) {
var now = new Date();
fn();
return new Date() - now;
},
spinOn = function(fn, i) {
return function() { for (var ii = i;ii;ii--) fn(); }
},
fn1_time = time(spinOn(fn1, i)),
fn2_time = time(spinOn(fn2, i));
console.log('function 1 took: %o', fn1_time);
console.log('function 2 took: %o', fn2_time);
};
Compare(
function() { var a = true, b = 1; if (a) b; },
function() { var a = true, b = 1; !a && b; },
10000000
);
Compare(
function() { var a = false, b = 1; if (a) b; },
function() { var a = false, b = 1; !a && b; },
10000000
);
Compare(
function() { var a = true, b = 1, c = 2; if (a) b; else c; },
function() { var a = true, b = 1, c = 2; !a && b; a && c; },
10000000
);
Compare(
function() { var a = false, b = 1, c = 2; if (a) b; else c; },
function() { var a = false, b = 1, c = 2; !a && b; a && c; },
10000000
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment