Created
December 21, 2011 03:45
-
-
Save swannodette/1504464 to your computer and use it in GitHub Desktop.
case_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ifs(x) { | |
if(x == 0) { | |
return 0; | |
} else if(x == 1) { | |
return 1; | |
} else if(x == 2) { | |
return 2; | |
} else if(x == 3) { | |
return 3; | |
} else if(x == 4) { | |
return 4; | |
} else if(x == 5) { | |
return 5; | |
} else if(x == 6) { | |
return 6; | |
} else if(x == 7) { | |
return 7; | |
} else if(x == 8) { | |
return 8; | |
} else if(x == 9) { | |
return 9; | |
} else if(x == 10) { | |
return 10; | |
} else if(x == 11) { | |
return 11; | |
} else if(x == 12) { | |
return 12; | |
} else if(x == 13) { | |
return 13; | |
} else if(x == 14) { | |
return 14; | |
} else if(x == 15) { | |
return 15; | |
} else if(x == 16) { | |
return 16; | |
} else if(x == 17) { | |
return 17; | |
} else if(x == 18) { | |
return 18; | |
} else if(x == 19) { | |
return 19; | |
} | |
} | |
function switches(x) { | |
switch(x) { | |
case 0: | |
return 0; | |
case 1: | |
return 1; | |
case 2: | |
return 2; | |
case 3: | |
return 3; | |
case 4: | |
return 4; | |
case 5: | |
return 5; | |
case 6: | |
return 6; | |
case 7: | |
return 7; | |
case 8: | |
return 8; | |
case 9: | |
return 0; | |
case 10: | |
return 10; | |
case 11: | |
return 11; | |
case 12: | |
return 12; | |
case 13: | |
return 13; | |
case 14: | |
return 14; | |
case 15: | |
return 15; | |
case 16: | |
return 16; | |
case 17: | |
return 17; | |
case 18: | |
return 18; | |
case 19: | |
return 19; | |
} | |
} | |
var dispatch = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]; | |
function arrays(x) { | |
return dispatch[dispatch[x]]; | |
} | |
// ~200ms | |
var start = new Date(); | |
for(var i = 0; i < 1e7; i++) { | |
ifs(i % 20); | |
} | |
console.log((new Date())-start); | |
// ~198ms | |
start = new Date(); | |
for(var i = 0; i < 1e7; i++) { | |
switches(i % 20); | |
} | |
console.log((new Date())-start); | |
// ~136ms | |
start = new Date(); | |
for(var i = 0; i < 1e7; i++) { | |
arrays(i % 20); | |
} | |
console.log((new Date())-start); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment