array vs switch
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var hash = {
0: 'a' ,
1: 'b' ,
2: 'c' ,
3: 'd' ,
4: 'e' ,
5: 'f' ,
6: 'g' ,
7: 'h' ,
8: 'a' ,
9: 'b' ,
10: 'a' ,
11: 'b' ,
12: 'c' ,
13: 'd' ,
14: 'e' ,
15: 'f' ,
16: 'g' ,
17: 'h' ,
18: 'a' ,
19: 'b' ,
20: 'a' ,
21: 'b' ,
22: 'c' ,
23: 'd' ,
24: 'e' ,
25: 'f' ,
26: 'g' ,
27: 'h' ,
28: 'a' ,
29: 'b'
},
value = 29,
getArray = function(){
return hash[value];
},
getSwitch = function(){
switch(value) {
case 0: return 'a'; break;
case 1: return 'b'; break;
case 2: return 'c'; break;
case 3: return 'd'; break;
case 4: return 'e'; break;
case 5: return 'f'; break;
case 6: return 'g'; break;
case 7: return 'h'; break;
case 8: return 'a'; break;
case 9: return 'b'; break;
case 10: return 'a'; break;
case 11: return 'b'; break;
case 12: return 'c'; break;
case 13: return 'd'; break;
case 14: return 'e'; break;
case 15: return 'f'; break;
case 16: return 'g'; break;
case 17: return 'h'; break;
case 18: return 'a'; break;
case 19: return 'b'; break;
case 20: return 'a'; break;
case 21: return 'b'; break;
case 22: return 'c'; break;
case 23: return 'd'; break;
case 24: return 'e'; break;
case 25: return 'f'; break;
case 26: return 'g'; break;
case 27: return 'h'; break;
case 28: return 'a'; break;
case 29: return 'b'; break;
}
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Array Access |
|
pending… |
Switch Access |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Jack
- Revision 2: published
- Revision 3: published
- Revision 4: published by Nico
0 comments