Prototype vs Non-Prototype

JavaScript performance comparison

Revision 13 of this test case created by Hyzhak

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;
    };
  }

  function getFullName() { return this.firstName + " " + this.lastName; };
  function Person3(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.getFullName = getFullName;
  }

  function Person4(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  Person4.prototype.firstName = '';
  Person4.prototype.lastName = '';
  Person4.prototype.getFullName = function() {
    return this.firstName + " " + this.lastName;
  };
</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
Prototype
var person = new Person("Jeremy", "McPeak");
pending…
Non-Prototype (Nested)
var person2 = new Person2("Jeremy", "McPeak");
pending…
Non-Prototype (Cached)
var person3 = new Person3("Jeremy", "McPeak");
pending…
j4
var person4 = new Person3();
person4("Jeremy", "McPeak")
pending…
complete prototype
var person4 = new Person4("Jeremy", "McPeak");
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