1arity constructors vs vanilla

Benchmark created by Perkee on


Setup

function VanillaPerson(name, surname,email,address,city,state,zip) {
    
      if (!(this instanceof VanillaPerson)) {
        return new VanillaPerson(name,surname,email,address,city,state,zip);
      }
    
      this.name    = name;
      this.surname = surname;
      this.email   = email;
      this.address = address;
      this.city    = city;
      this.state   = state;
      this.zip     = zip;
    }
    
    function Person(obj) {
    
      if (!(this instanceof Person)) {
        return new VanillaPerson(obj);
      }
    
      this.name    = obj.name;
      this.surname = obj.surname;
      this.email   = obj.email;
      this.address = obj.address;
      this.city    = obj.city;
      this.state   = obj.state;
      this.zip     = obj.zip;
    }
    
    function struct(props) {
    
      function Struct(obj) {
        // make `new` optional
        if (!(this instanceof Struct)) return new Struct(obj);
        // add props
        var name;
        for ( var i = 0, len = props.length ; i < len ; i++ ) {
          name = props[i];
          this[name] = obj[name];
        }
      }
      return Struct;
    }
    
    var StructPerson = struct(['name','surname','email','address','city','state','zip']);
    
    var person;

Test runner

Ready to run.

Testing in
TestOps/sec
new VanillaPerson
person = new VanillaPerson('Giulio', 'Canti','giulio@canti.com','123 Fake Street','Springfield','XX','00000');
ready
new Person
person = new Person({
  name   : 'Giulio',
  surname: 'Canti',
  email  : 'giulio@canti.com',
  address: '123 Fake Street',
  city   : 'Springfield',
  state  : 'XX',
  zip    : '00000'
});
ready
new StructPerson
person = new StructPerson({
  name   : 'Giulio',
  surname: 'Canti',
  email  : 'giulio@canti.com',
  address: '123 Fake Street',
  city   : 'Springfield',
  state  : 'XX',
  zip    : '00000'
});
ready
VanillaPerson
person = VanillaPerson('Giulio', 'Canti','giulio@canti.com','123 Fake Street','Springfield','XX','00000');
ready
Person
person = Person({
  name   : 'Giulio',
  surname: 'Canti',
  email  : 'giulio@canti.com',
  address: '123 Fake Street',
  city   : 'Springfield',
  state  : 'XX',
  zip    : '00000'
});
ready
StructPerson
person = StructPerson({
  name   : 'Giulio',
  surname: 'Canti',
  email  : 'giulio@canti.com',
  address: '123 Fake Street',
  city   : 'Springfield',
  state  : 'XX',
  zip    : '00000'
});
ready

Revisions

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

  • Revision 1: published by Perkee on