Switch vs. hash
JavaScript performance comparison
Info
Is a hash (array) really faster than a switch?
Preparation code
<script>
var theVar = 'j';
var cases = [];
cases['a'] = function() { var i = 1; i+=1; return i; };
cases['b'] = function() { var i = 1; i+=1; return i; };
cases['c'] = function() { var i = 1; i+=1; return i; };
cases['d'] = function() { var i = 1; i+=1; return i; };
cases['e'] = function() { var i = 1; i+=1; return i; };
cases['f'] = function() { var i = 1; i+=1; return i; };
cases['g'] = function() { var i = 1; i+=1; return i; };
cases['h'] = function() { var i = 1; i+=1; return i; };
cases['i'] = function() { var i = 1; i+=1; return i; };
cases['j'] = function() { var i = 1; i+=1; return i; };
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Switch |
|
pending… |
Hash |
|
pending… |
Hash-cached |
|
pending… |
Try catch |
|
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 2: published by David Calhoun
- Revision 4: published
- Revision 7: published by Kevin Decker
- Revision 8: published by layane
- Revision 10: published
- Revision 11: published
- Revision 14: published by Maciej Pawłowski
- Revision 16: published
- Revision 17: published
- Revision 18: published
0 comments