Simple Object Creation Overhead

Benchmark created by Damon Oehlman on


Description

A test of the overheads of creating objects using an object literal costs as opposed to using a predefined prototype and constructor.

Preparation HTML

<script>
  function XY(x, y) {
   this.x = x || 0;
   this.y = y || 0;
  }
  
  XY.prototype = {
   constructor: XY,
   toString: function() {
    return 'x: ' + this.x + ', y: ' + this.y;
   }
  };
  
  function initXY(x, y) {
   return {
    x: x || 0,
    y: y || 0
   };
  }
  
  function initXYAndMethod(x, y) {
   x = x || 0;
   y = y || 0;
  
   return {
    x: x,
    y: y,
    toString: function() {
     return 'x: ' + x + ', y: ' + y;
    }
   };
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
New Object Literal
initXY(5, 10);
ready
New Literal with Method
initXYAndMethod(5, 10);
ready
New Predefined Class
new XY(5, 10);
ready

Revisions

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

  • Revision 1: published by Damon Oehlman on