factory vs constructor (v3)

Revision 3 of this benchmark created 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.animal = 'monkey';
      this.number = 8000;
    }
    
    function testFactory() {
      return {
        test: true,
        animal: 'monkey',
        number: 8000
      };
    }
    
    function TestConstructorWithParams(test, animal, number) {
        this.test = test;
        this.animal = animal;
        this.number = number;
    }
    
    function testFactoryWithParams(test, animal, number) {
        return {
            test: test,
            animal: animal,
            number: number
        }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
new
new TestConstructor();
ready
object literal
testFactory();
ready
new + params
new TestConstructorWithParams(true, 'monkey', 8000);
ready
factory + params
testFactoryWithParams(true, 'monkey', 8000);
ready

Revisions

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