perf impact of passing references

JavaScript performance comparison

Revision 12 of this test case created and last updated

Info

This test is attempting to show the performance impact of passing references compared to normal scope chain lookups.

Check out Compact.js, that's based on Require.js

Dominik Guzei Wizzart by the way -> I'm searching for a JavaScript internship :)

Modified By

@jdalton

Preparation code

<script>
  ui.benchmarks[0].setup = function() {
   var Library = {
    'util': {
     'add': function(a, b) {
      return a + b;
     }
    },
    'cool': {
     'Class': function(startValue) {
      this.value = startValue;
     }
    }
   };
 
   Library.cool.Class.prototype = {
    'addToValue': function(value) {
     this.value = Library.util.add(this.value, value);
    }
   };
  };
 
 
  ui.benchmarks[1].setup = function() {
   var cache = {};
 
   function define(namespace, includes, callback) {
    var i = -1;
    if (arguments.length < 3) {
     callback = includes;
     includes = [];
    }
    // convert string namespaces to objects
    while (includes[++i]) {
     includes[i] = cache[includes[i]];
    }
    cache[namespace] = callback.apply(null, includes);
   }
 
   function require(includes, callback) {
    var i = -1;
 
    // convert string namespaces to objects
    while (includes[++i]) {
     includes[i] = cache[includes[i]];
    }
    callback.apply(null, includes);
   }
 
   define('Library.util.add', function() {
    function add(a, b) {
     return a + b;
    }
    return add;
   });
 
   define('Library.cool.Class', ['Library.util.add'], function(add) {
    function Class(startValue) {
     this.value = startValue;
    }
    Class.prototype = {
     'addToValue': function(value) {
      this.value = add(this.value, value);
     }
    };
    return Class;
   });
 
   var Klass;
   require(['Library.cool.Class'], function(Class) {
    Klass = Class;
   });
  };
 
 
  ui.benchmarks[2].setup = ui.benchmarks[1].setup;
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Normal Lookup
(new Library.cool.Class(1)).addToValue(1);
pending…
Local Lookup
(new Klass(1)).addToValue(1);
pending…
Passing+Require
require(['Library.cool.Class'], function(Class) {
 (new Class(1)).addToValue(1);
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment