factory vs constructor

Benchmark created by Matt Baker on


Description

When instantiating objects of the same class, is it faster to use the new keyword or just object literals?

Setup

function TestConstructor() {
      this.test = true;
      this.person = 'Matt';
      this.number = 9000;
    }
    
    function testFactory() {
      return {
        test: true,
        animal: 'monkey',
        number: 8000
      };
    }

Test runner

Ready to run.

Testing in
TestOps/sec
new
new TestConstructor();
ready
object literal
testFactory();
ready

Revisions

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