Debunk constructor vs prototype method declaration

JavaScript performance comparison

Test case created by Raymond

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
MyClass = function(){
  this.localMethod = function(){ }
MyClass = function(){};
MyClass.prototype.localMethod = function(){ }

Compare results of other browsers


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

1 Comment

A. Matías Quezada commented :

Of course the first declaration is faster, that's because the first one is creating a one function per iteration, the second one is creating two functions and resolving MyClass.prototype.localMethod and assigns it.

That means you can create "classes" faster with the closure system, but it says nothing about instanciation, when you instanciate this classes the second one (prototyped) just creates a prototyped object. The first one creates an object, creates a function, resolves this.localMethod and assigns it, on each instanciation.