Creating Instances vs. Static Functions

JavaScript performance comparison

Revision 3 of this test case created

Info

Repeatedly creating instances of a class is very slow and inefficient.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function add(a, b) {
      return a + b;
    }
   
    function Class() {}
    Class.prototype.add = add;
   
    var obj = {}
    obj.add = add;
};
</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
With call to constructor (member function)
var instance = new Class();
var foo = instance.add(1337, Math.random());
pending…
Without call to constructor (static function)
var foo = add(1337, Math.random());
pending…
obj
var foo = obj.add(1337, Math.random());
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