Switch vs. If + else
JavaScript performance comparison
Revision 3 of this test case created
Preparation code
<script type="text/javascript">
var n = 100,
c = 'a',
s = 'string test';
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Testing in unknown unknown
| Test |
Ops/sec |
Switch Number |
switch (n) { case 10: break; case 20: break; case 30: break; case 40: break; case 50: break; case 60: break; case 70: break; case 80: break; case 90: break; case 100: break; };
|
pending… |
If/Else Number |
if (n === 10) { } else if (n === 20) { } else if (n === 30) { } else if (n === 40) { } else if (n === 50) { } else if (n === 60) { } else if (n === 70) { } else if (n === 80) { } else if (n === 90) { } else if (n === 100) { };
|
pending… |
Switch Char |
switch (c) { case 'j': break; case 'i': break; case 'h': break; case 'g': break; case 'f': break; case 'e': break; case 'd': break; case 'c': break; case 'b': break; case 'a': break; };
|
pending… |
If/Else Char |
if (c === 'j') { } else if (c === 'i') { } else if (c === 'h') { } else if (c === 'g') { } else if (c === 'f') { } else if (c === 'e') { } else if (c === 'd') { } else if (c === 'c') { } else if (c === 'b') { } else if (c === 'a') { };
|
pending… |
Switch String |
switch (s) { case 'gntsiretts': break; case 'stteristng': break; case 'strteingst': break; case 'testrsting': break; case 'testringst': break; case 'strtesting': break; case 'teststring': break; case 'stringtest': break; case 'test string': break; case 'string test': break; };
|
pending… |
If/Else String |
if (s === 'gntsiretts') { } else if (s === 'stteristng') { } else if (s === 'strteingst') { } else if (s === 'testrsting') { } else if (s === 'testringst') { } else if (s === 'strtesting') { } else if (s === 'teststring') { } else if (s === 'stringtest') { } else if (s === 'test string') { } else if (s === 'string test') { };
|
pending… |
Compare results of other browsers
0 comments