Prototype vs Non-Prototype

JavaScript performance comparison

Revision 16 of this test case created by r56

Info

Testing the difference in execution time of creating and accessing 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");
var fullName = person.getFullName();
pending…
Non-Prototype (Nested)
var person = new Person2("Jeremy", "McPeak");
var fullName = person.getFullName();
pending…
Non-Prototype (Cached)
var person = new Person3("Jeremy", "McPeak");
var fullName = person.getFullName();
pending…
Prototype (with fields)
var person = new Person4("Jeremy", "McPeak");
var fullName = person.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