prototype chain lookup, cached or not

Benchmark created by Kyle Simpson on


Description

Testing/profiling the code patterns from the Script Junkie article "(pre)Maturely Optimize Your JavaScript"

http://msdn.microsoft.com/en-us/scriptjunkie/gg622887.aspx

Snippet comparison #5

Preparation HTML

<script>
  if (!Object.create) {
   Object.create = function(o) {
    function F() {}
    F.prototype = o;
    return new F();
   };
  }
  
  
  var foo = {
   fun: "weee!"
  },
      bar = Object.create(foo),
      baz = Object.create(bar),
      _fun = baz.fun,
      res;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
full chain lookup
res = "";

for (var i = 0; i < 1000; i++) {
 res += baz.fun[i % baz.fun.length]; // not using the cached chain lookup
}
ready
cached chain lookup
res = "";

for (var i = 0; i < 1000; i++) {
 res += _fun[i % _fun.length]; // using the cached chain lookup
}
ready

Revisions

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

  • Revision 1: published by Kyle Simpson on