Closures vs functions

JavaScript performance comparison

Revision 6 of this test case created by shkdee

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function f(w, x, y, z) {
      var v = w + x + y + z
      return v
    }
   
    function cl_f(w, x, y, z) {
      return function() {
        var v = w + x + y + z
        return v
      }
    }
   
    function obj_f(o) {
      var v = o.w + o.x + o.y + o.z
      return v
    }
   
    function arr_f(a) {
      var v = a[0] + a[1] + a[2] + a[3]
      return v
    }
   
    function obj(w, x, y, z) {
      this.w = w
      this.x = x
      this.y = y
      this.z = z
    }
};
</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
Closures
var a = (function() {
  return f(1, 2, 3, 4)
})
a()
pending…
Closures from functions
var a = cl_f(1, 2, 3, 4)
a()
pending…
Arrays
var a = [1, 2, 3, 4]
arr_f(a)
pending…
Objects
var a = new obj(1, 2, 3, 4)
obj_f(a)
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