if-switch

Benchmark created by if-switch on


Setup

function testIf(x) {
      if (x === 'a') {
        console.log('a');
      } else if (x === 'b') {
        console.log('b');
      } else if (x === 'c') {
        console.log('c');
      } else if (x === 'd') {
        console.log('d');
      } else if (x === 'e') {
        console.log('e');
      } else if (x === 'f') {
        console.log('f');
      } else if (x === 'g') {
        console.log('g');
      } else {
        console.log('unknown');
      }
    }
    
    function testSwitch(x) {
      switch (x) {
        case 'a':
          console.log('a');
          break;
        case 'b':
          console.log('b');
          break;
        case 'c':
          console.log('c');
          break;
        case 'd':
          console.log('d');
          break;
        case 'e':
          console.log('e');
          break;
        case 'f':
          console.log('f');
          break;
        case 'g':
          console.log('g');
          break;
        default:
          console.log('unknown');
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
if
testIf('a');
testIf('d');
testIf('g');
testIf('x');
ready
switch
testSwitch('a');
testSwitch('d');
testSwitch('g');
testSwitch('x');
ready

Revisions

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

  • Revision 1: published by if-switch on