Prototype vs Non-Prototype

JavaScript performance comparison

Revision 2 of this test case created

Info

Testing the difference in execution time of creating objects using the prototype and not using it.

Preparation code

<script>
  function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
 
  Person.prototype.getFullName = function() {
    return this.firstName + " " + this.lastName;
  };
 
  function Person2(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.getFullName = function() {
      return this.firstName + " " + this.lastName;
    };
  }
</script>
<span id="t1"></span> - <span id="t2"></span>
<script>
Benchmark.prototype.setup = function() {
    var name;
    var name2;
};

Benchmark.prototype.teardown = function() {
    var t1 = document.getElementById('t1');
    var t2 = document.getElementById('t2');
   
    t1.innerHTML = name;
    t2.innerHTML = name2;
};
</script>

Preparation code output

-

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Prototype
var person = new Person("Jeremy", "McPeak");
name = person.getFullName();
pending…
Non-Prototype (Nested)
var person2 = new Person2("Jeremy", "McPeak");
name2 = person2.getFullName();
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