Object literal vs custom constructor (v3)

Revision 3 of this benchmark created on


Setup

var Foo1 = function(prop) {
      this.bar = prop;
    };
    
    function Foo2(prop) {
      this.bar = prop;
    }
    
    var foo3 = {
      bar: ""
    };
    
    function foo4(prop) {
      return {
        bar: prop
      };
    }
    
    function foo5(prop) {
      return Object.freeze({
        bar: prop
      });
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Object literal
var y = {bar: 42};
ready
new1
var y = new Foo1(42);
ready
new2
var y = new Foo2(42);
ready
.create
var y = Object.create(foo3);
y.bar = 42;
ready
instantiate
var y = foo4(42);
ready
instantiate + freeze
var y = foo5(42);
ready

Revisions

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