Creating Instances vs. Static Functions

JavaScript performance comparison

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();
instance.add(1337, 420);
Without call to constructor (static function)
add(1337, 420);

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