Creating Instances vs. Static Functions

JavaScript performance comparison

Revision 2 of this test case created


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

Preparation code

Benchmark.prototype.setup = function() {
    function add(a, b) {
      return a + b;
    function Class() {}
    Class.prototype.add = add;

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());
Without call to constructor (static function)
var foo = add(1337, Math.random());

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment