ifelse versus switch

JavaScript performance comparison

Revision 2 of this test case created by Supercryptonite

Info

Check performance of switch and if else with inline and referenced variables

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var f, g, h;
    f = 0, g = 1, h = 2;
};
</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
If/Else inline
var v = 2;
if (v === 0) {} else if (v === 1) {} else if (v === 2) {}
pending…
If/Else reference
var v = 2;
if (v === f) {} else if (v === g) {} else if (v === h) {}
pending…
Switch inline
var v = 2;
switch (v) {
  case 0:
    break;
  case 1:
    break;
  case 2:
    break;
}
pending…
Switch reference
var v = 2;
switch (v) {
  case f:
    break;
  case g:
    break;
  case h:
    break;
}
pending…
Tenary inline (approx)
var v = 2;
var result = v === 0 ? true : v === 1 ? true : v === 2 ? true : false;
pending…
Tenary reference (approx)
var v = 2;
var result = v === f ? true : v === g ? true : v === h ? true : false;
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:

0 comments

Add a comment