add

JavaScript performance comparison

Test case created by Prince Neil Cedrick Castro

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let sumSwitchCase = function(a,b,c,d,e,f,g,h,i,j,...args) {
    const argLen = arguments.length;
    switch (argLen) {
      case 0:
      case 1: return undefined; break;
      case 2: return a + b;break;
      case 3: return a + b + c;break;
      case 4: return a + b + c + d;break;
      case 5: return a + b + c + d + e;break;
      case 6: return a + b + c + d + e + f;break;
      case 7: return a + b + c + d + e + f + g;break;
      case 8: return a + b + c + d + e + f + g + h;break;
      case 9: return a + b + c + d + e + f + g + h + i;break;
      case 10: return a + b + c + d + e + f + g + h + i + j;break;
      default: {
        let len = args.length;
        let result = a + b + c + d + e + f + g + h + i + j;
        while (len) { result += args[--len] }
        return result;
      }
    }
  };
  
  let sumSwitchCaseArguments = function(a,b,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,...args) {
    const argLen = arguments.length;
    switch (argLen) {
      case 0:
      case 1: return undefined; break;
      case 2: return a + b; break;
      case 3: return a + b + c; break;
      case 4: return a + b + c + d; break;
      case 5: return a + b + c + d + e; break;
      case 6: return a + b + c + d + e + f; break;
      case 7: return a + b + c + d + e + f + g; break;
      case 8: return a + b + c + d + e + f + g + h; break;
      case 9: return a + b + c + d + e + f + g + h + i; break;
      case 10: return a + b + c + d + e + f + g + h + i + j; break;
      default: {
        let len = args.length;
        let result = a + b + c + d + e + f + g + h + i + j;
        while (len) { result += args[--len] }
        return result;
      }
    }
  };
  
  let sumIfElse = function(a,b,c,d,e,f,g,h,i,j,...args) {
    if (arguments.length<2) {
      return undefined;
    }
    if (c) {
      if (d) {
        if (e) {
          if (f) {
            if (g) {
              if (h) {
                if (i) {
                  if (j) {
                    if (args.length) {
                        let len = args.length;
                        let result = a+b+c+d+e+f+g+h+i+j;
                        while(len) { result += args[--len] }
                        return result;
                    }
                    return a+b+c+d+e+f+g+h+i+j;
                  }
                  return a+b+c+d+e+f+g+h+i
                }
                return a+b+c+d+e+f+g+h
              }
              return a+b+c+d+e+f+g
            }
            return a+b+c+d+e+f
          }
          return a+b+c+d+e
        }
        return a+b+c+d
      }
      return a+b+c
    }
    return a + b
  };
  
  let sumLookup = function(a,b,c,d,e,f,g,h,i,j,...args) {
    let _b = a+b;
    let _c = _b+c;
    let _d = _c+d;
    let _e = _d+e;
    let _f = _e+f;
    let _g = _f+g;
    let _h = _g+h;
    let _i = _h+i;
    let _j = _i+j;
    let results = {a,_b,_c,_d,_e,_f,_g,_h,_i,_j};
    let argLen = arguments.length;
    if (argLen < 11) {
      return results[argLen-1]
    }
    let len = args.length;
    let result = _j;
    while(len) { result += args[--len] }
    return result;
  }
  
  let sumReduce = (...args) => args.reduce((a,b) => a+b,0)
  
  let arrToAdd = [12,24,36,48,60,72,84,96,108];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
sum with switch-case
sumSwitchCase(12,24,36,48,60,72,84,96,108);
pending…
sum with if-else
sumIfElse(12,24,36,48,60,72,84,96,108);
pending…
sum with switch-case arguments
sumSwitchCaseArguments(12,24,36,48,60,72,84,96,108);
pending…
sum with arr lookup
sumLookup(12,24,36,48,60,72,84,96,108)
pending…
sum with reduce
sumReduce(12,24,36,48,60,72,84,96,108)
pending…
sum with switch-case with arr
sumSwitchCase(...arrToAdd)
pending…
sum with reduce with array
sumReduce(arrToAdd)
pending…
sum with if-else with arr
sumIfElse(...arrToAdd)
pending…

Revisions

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

0 Comments