Skip to content

Instantly share code, notes, and snippets.

@miketaylr
Created July 6, 2010 17:47
Show Gist options
  • Save miketaylr/465681 to your computer and use it in GitHub Desktop.
Save miketaylr/465681 to your computer and use it in GitHub Desktop.
function benchmark(method, times, name){
//See http://gist.github.com/227048
var startTime = (new Date()).getTime(), endTime;
while(times--){
method();
}
endTime = (new Date()).getTime();
console.log(name, endTime - startTime);
}
var x = 1, y = 0;
benchmark(function(){
y = (x === 0 ? x : x === 1);
}, 100000, "Ternary Operator");
x = 1;
y = 0;
benchmark(function(){
switch(x){
case 0: y = x; break;
case 1: y = x; break;
default:y = x; break;
}
}, 100000, "switch Statement");
@miketaylr
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment