Slide 1

Slide 1 text

Object Creation Pattern Performance Andy Appleton @appltn http://appleton.me

Slide 2

Slide 2 text

Two popular patterns for making objects

Slide 3

Slide 3 text

Module Pattern var Module = function(arg){ var self = { getArg: function(){ return arg1; } }; return self; };

Slide 4

Slide 4 text

Constructor Functions var Constructor = function(arg){ this.arg = arg; }; Constructor.prototype.getArg = function(){ return this.arg; };

Slide 5

Slide 5 text

Smackdown vs

Slide 6

Slide 6 text

jsperf.com

Slide 7

Slide 7 text

Test 1: Object Instantiation var doug = new Module('arg'); var bono = new Constructor('arg');

Slide 8

Slide 8 text

Test 1: Object Instantiation Chrome 20 Firefox 13 IE 9 Safari 5.1 Module Constructor +30% (operations/sec)

Slide 9

Slide 9 text

0 - 1

Slide 10

Slide 10 text

Test 2: Method Calls doug.getArg(); bono.getArg();

Slide 11

Slide 11 text

Test 2: Method Calls Chrome 20 Firefox 13 IE 9 Safari 5.1 Module Constructor +57% (operations/sec)

Slide 12

Slide 12 text

1 - 1

Slide 13

Slide 13 text

Test 2.5: Instantiation & Method Calls var doug = new Module('arg'); doug.getArg(); var bono = new Constructor('arg'); bono.getArg();

Slide 14

Slide 14 text

Chrome 20 Firefox 13 IE 9 Safari 5.1 Test 2.5: Instantiation & Method Calls Module Constructor +61% (operations/sec)

Slide 15

Slide 15 text

1 - 2

Slide 16

Slide 16 text

Test 3: Instantiation with inheritance var Module = function(arg1){ // Set up and return self object }; var ModuleChild = function(arg1, arg2){ var self = new Module(arg1); self.method = function(){}; return self; };

Slide 17

Slide 17 text

Test 3: Instantiation with inheritance var Constructor = function(arg1){}; // Add methods to Constructor.prototype var Child = function(arg1, arg2){ // Add instance variables to `this` }; Child.prototype = new Constructor(this.arg1); Child.prototype.method = function(){};

Slide 18

Slide 18 text

Test 3: Instantiation with inheritance var doug = new ModuleChild('arg', 'arg'); var bono = new ConstructorChild('arg', 'arg');

Slide 19

Slide 19 text

Chrome 20 Firefox 13 IE 9 Safari 5.1 Test 3: Instantiation with inheritance Module Constructor +93% (operations/sec)

Slide 20

Slide 20 text

1 - 3

Slide 21

Slide 21 text

Test 4: Method calls with inheritance doug.getArg1(); doug.getArg2(); bono.getArg1(); bono.getArg2();

Slide 22

Slide 22 text

Chrome 20 Firefox 13 IE 9 Safari 5.1 child method parent method child method parent method Test 4: Method calls with inheritance Module Constructor +56% +53% (operations/sec)

Slide 23

Slide 23 text

2 - 3

Slide 24

Slide 24 text

Test 4.5: Instantiation & Method calls with inheritance var doug1 = new ModuleChild('arg', 'arg'); doug1.getArg1(); var doug2 = new ModuleChild('arg', 'arg'); doug2.getArg2(); var bono1 = new ConstructorChild('arg', 'arg'); bono1.getArg1(); var bono2 = new ConstructorChild('arg', 'arg'); bono2.getArg2();

Slide 25

Slide 25 text

Chrome 20 Firefox 13 IE 9 Safari 5.1 child method parent method child method parent method Test 4.5: Instantiation & Method calls with inheritance Module Constructor +92% +91% (operations/sec)

Slide 26

Slide 26 text

2 - 4

Slide 27

Slide 27 text

Constructor functions are faster to instantiate Use constructors if you’re going to have a lot of objects

Slide 28

Slide 28 text

Modules are quicker to call methods on Use modules if you’re going to be calling a lot of methods

Slide 29

Slide 29 text

Other factors...

Slide 30

Slide 30 text

Bono > Crockford and apparently...

Slide 31

Slide 31 text

1. Object instantiation http://jsperf.com/object-instantiation-test 2. Method calls on instantiated object http://jsperf.com/method-calling-test 2.5. Object instantiation and method calls http://jsperf.com/intantiation-and-method-call-test 3. Object instantiation with inheritance http://jsperf.com/instantiation-with-inheritance-test 4. Method calls on instantiated objects with inheritance http://jsperf.com/method-calling-with-inheritance-test 4.5. Object instantiation and method calls with inheritance http://jsperf.com/instantiation-and-method-call-test-with-inheritance