Eval vs dynamic function

Benchmark created by Enzo on


Setup

User = {
        filter: function(a,b){ console.log(arguments); return this;},
        order_by: function(a,b){alert(1);return a+b;}
    };
    
    
    rules = [
      ['User', null],
      ['filter', [{ idgroup: 95, active: true}]],
      ['order_by', ['name', 'created']]
    ];

Test runner

Ready to run.

Testing in
TestOps/sec
Dynamic
// Or whatever the first rule will invoked upon
result = window;

// Apply each rule in turn
rules.forEach(function (rule) {
    var prop = rule[0];
    var value = rule[1];
    // All values expected null/undefined
    if (value === null) {
        // Property look-up only
        result = result[prop];
    } else {
        // Call the function with the same name and apply rule arguments
        result = result[prop].apply(result, value);
    }
});

// Use result:
console.log(result);
ready
eval
eval("User.filter({ idgroup: 95, active: true}).order_by('name', 'created')");
ready

Revisions

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

  • Revision 1: published by Enzo on