if vs switch2
JavaScript performance comparison
Preparation code
<script src="https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js">
</script>
<script>
var a = 'hello',
b = 'world',
foo1, foo2;
function test1(pValue) {
if (pValue == a) return 'a'
else if (pValue == b) return 'b'
}
function test2(pValue) {
switch (pValue) {
case a:
return 'a';
break;
case b:
return 'b';
break;
}
}
function test3(pValue) {
switch (true) {
case / hel / .test(pValue):
return 'a';
break;
case / wo / .test(pValue):
return 'b';
break;
}
}
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
if, else |
|
pending… |
case 1 |
|
pending… |
case2 |
|
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
- Revision 2: published
0 comments