Static variables in class vs in closures

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Static variable
  Benchmark.prototype.setup = function() {
    // counter as an object
    var counter1 = function() {
      return counter1.count = ((counter1.count + 1) % 100 || 0)
    }
  };
ready
Closure
  Benchmark.prototype.setup = function() {
    //counter as an anonymous function
    var counter2 = (function() {
      var count = 0  // static private closured variable
      return function() { return count = (count + 1) % 100 }
    })()
  };
    
ready

Revisions

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