Localized function with closure vs. lookup in for loop

Benchmark created by Kim Trott on


Description

Compare localizing a function that needs this context maintained in closure vs lookup every time in foor loop

Preparation HTML

<script>
 var iters = 100;
 
 var testClass = {
  exVar: 1,
 
  funcA: function() {
   this.exVar + 1;
  }
 };
 
 var proxy = function(fn, scope) {
  if (!scope) scope = this;
  return function() {
   return fn.apply(scope, arguments);
  };
 };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Lookup Func in For Loop
var i = 0;

for (; i < iters; i++) {
 testClass.funcA();
}
ready
Use Closure to Localize
var i = 0;

var funcAClosure = proxy(testClass.funcA, testClass);

for (; i < iters; i++) {
 funcAClosure();
}
ready

Revisions

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

  • Revision 1: published by Kim Trott on