Dynamic functions 1

Benchmark created on


Setup

var ops = ['+', '-', '*', '/']
    var functions1 = []
    var functions2 = []
    
    for (var i = 0; i < 4; ++i) {
       ind = i % 4;
       if (ops[ind] === '+') {
          functions1.push(function (x) {
              return 5 + x;
          });
       }
       if (ops[ind] === '-') {
          functions1.push(function (x) {
              return 5 - x;
          });
       }
       if (ops[ind] === '*') {
          functions1.push(function (x) {
              return 5 * x;
          });
       }
       if (ops[ind] === '/') {
          functions1.push(function (x) {
              return 5 / x;
          });
       }
       eval("functions2.push(function (x) {return 5 " + ops[ind] + " x;})");
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Static
for (var i = 0; i < 4; ++i) {
    functions1[i](2);
}
ready
Dynamic
for (var i = 0; i < 4; ++i) {
    functions2[i](2);
}
ready

Revisions

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