Case Inside Loop (v5)

Revision 5 of this benchmark created on


Description

Checking the performance of case statement inside while loop vs conventional structures.

Setup

var lcl, ct, jump;
    
    var sequence = {};
    
    sequence[1] = (function() {
      lcl = 1;
      return 2;
    });
    
    sequence[2] = (function() {
      if (lcl >= 1000) {
        return 3;
      }
      lcl++;
      return 2;
    });
    
    sequence[3] = (function() {
      if (lcl <= 0) {
        return 4;
      }
      lcl--;
      return 3;
    });
    
    sequence[4] = (function() {
      return undefined;
    });
    
    var run = (function(pt) {
      while (pt) pt = sequence[pt]();
    });

Test runner

Ready to run.

Testing in
TestOps/sec
For Loop
lcl = 1;

for (ct = 0; ct < 1000; ct++) {
  lcl++;
}

for (ct = 0; ct < 1000; ct++) {
  lcl--;
}
ready
Case + Loop
var jump = 0x01;

while (jump) {
  switch (jump) {
  case 0x01:
    lcl = 1;
    jump = 0x02;
  case 0x02:
    if (lcl >= 1000) {
      jump = 0x03;
      break;
    }
    lcl++;
    break;
  case 0x03:
    if (lcl <= 0) {
      jump = 0x04;
      break;
    }
    lcl--;
    break;
  case 0x04:
  default:
    jump = undefined;
    break;
  }
}
ready
While Loop
lcl = 1;
while (lcl < 1000) {
  lcl++;
}
while (lcl > 0) {
  lcl--;
}
ready
For + Switch
var jump = 0x01;

out: for (;;) {
  switch (jump) {
  case 0x01:
    lcl = 1;
    jump = 0x02;
  case 0x02:
    if (lcl >= 1000) {
      jump = 0x03;
      break;
    }
    lcl++;
    break;
  case 0x03:
    if (lcl <= 0) {
      jump = 0x04;
      break;
    }
    lcl--;
    break;
  case 0x04:
  default:
    break out;
  }
}
ready
Chained Functions
run(1);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.