74
How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer Mathias Bynens – Velocity Europe, November 2011

How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Embed Size (px)

Citation preview

Page 1: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

How DRY impacts JavaScript performance //Faster JavaScript execution for the lazy developer

Mathias Bynens – Velocity Europe, November 2011

Page 2: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

@mathias

Page 3: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

JavaScript & performance

Rule #1: nothing to do with JS

Page 4: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

JavaScript & performance

Page 5: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

JavaScript & performance

What about

the actual run-time performance

on the client side?

Page 6: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DRYflic.kr/p/2ZGCT

Page 7: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

WETflic.kr/p/5Jnj7Q

Page 8: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

“DRY leads to readable, maintainable code”

Page 9: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DRY JavaScript improves

performance…if you do it right

Page 10: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

So, where to avoid repetition?

Page 11: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

What’s slow in JavaScript?

Page 12: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

What’s slow in JavaScript?

1. The DOM

Page 13: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

What’s slow in JavaScript?

1. The DOM

2. Function calls

Page 14: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

What’s slow in JavaScript?

1. The DOM

2. Function calls

3. Lookups

Page 15: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulation// Create the element in memoryvar el = document.createElement('p');

// Insert the element into the DOMdocument.body.appendChild(el);

Page 16: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulation<body> … <div> <p></p> </div></body>

Page 17: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulationvar div = document.createElement('div'), p = document.createElement('p');

// Baddocument.body.appendChild(div);div.appendChild(p);

Page 18: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulationvar div = document.createElement('div'), p = document.createElement('p');

// Betterdiv.appendChild(p);document.body.appendChild(div);

Page 19: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulation<body> … <p></p> <p></p> <p></p> <p></p></body>

Page 20: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulationvar p = document.createElement('p'),

i = 4;

while (i--) { // Add four <p> elements

document.body.appendChild(p.cloneNode(false));

}

Page 21: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

DOM manipulationvar frag = document.createDocumentFragment(), p = document.createElement('p'), i = 4;

while (i--) { // Add four <p> elements frag.appendChild(p.cloneNode(false));}

document.body.appendChild(frag);

Page 22: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Function calls// Function declaration

function foo(bar) {

return bar;

}

// Function call

foo('something');

Page 23: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Function callsalert('foo');document.getElementById('foo');$('#foo');

Page 24: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Function calls$('.foo').show();// other stuff…$('.foo').hide();

Page 25: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Function callsvar $foo = $('.foo');$foo.show();// other stuff…$foo.hide();

Page 26: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Function callsvar $foo = $('.foo').show();// other stuff…$foo.hide();

Page 27: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Property lookupsvar obj = { 'x': 42, 'y': { 'foo': 'bar' }};

obj.x; // 42obj.y.foo; // 'bar'

Page 28: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Property lookupsdocument.title

dojo.query(…)

YAHOO.util.Dom.get(…)

Page 29: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Property lookupsvar foo = YAHOO.util.Dom.get('foo'), bar = YAHOO.util.Dom.get('bar'), baz = YAHOO.util.Dom.get('baz'), qux = YAHOO.util.Dom.get('qux');

Page 30: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Property lookupsvar get = YAHOO.util.Dom.get, foo = get('foo'), bar = get('bar'), baz = get('baz'), qux = get('qux');

Page 31: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Array item lookupsvar elems = document.getElementsByTagName('p'), length = elems.length;

while (length--) { if (elems[length].className == 'foo') { // do something with elems[length] elems[length].innerHTML = 'LOLWAT'; }}

Page 32: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Array item lookupsvar elems = document.getElementsByTagName('p'), length = elems.length, elem;

while (length--) { elem = elems[length]; if (elem.className == 'foo') { // do something with elem elem.innerHTML = 'LOLWAT'; }}

Page 33: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookupsvar foo = 42;foo; // no scope lookup

Page 34: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookupsvar foo = 42;(function() { foo; // one scope lookup}());// IIFE – see http://mths.be/iife

Page 35: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookupsvar foo = 42;(function() { (function() { foo; // two scope lookups }());}());

Page 36: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookups

Page 37: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookupsvar foo = 42;(function(foo) { (function(foo) { foo; // ZOMG, no scope lookups!!1 }(foo));}(foo));

Page 38: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookups

Page 39: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookups(function() { // every time you use `window` // or `document` here // that’s a scope lookup}());

Page 40: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookups(function() { var doc = document, win = window; // lookup once, then cache}());

Page 41: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Scope lookups(function(win, doc) { // use `win` and `doc` here // no scope lookups // no performance penalty!}(this, document));

Page 42: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Recap: what’s slow in JavaScript?

Page 43: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Recap: what’s slow in JavaScript?

1. The DOM

Page 44: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Recap: what’s slow in JavaScript?

1. The DOM

2. Function calls

Page 45: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Recap: what’s slow in JavaScript?

1. The DOM

2. Function calls

3. Lookups

Page 46: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Especially when used inside…

Page 47: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Especially when used inside…

• Loops

Page 48: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Especially when used inside…

• Loops

• Intervals

Page 49: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Especially when used inside…

• Loops

• Intervals

• Handlers for events that fire frequently

Page 50: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

It happens to the best!// Don’t do this:$(window).scroll(function() { $('.foo').something();});

Page 51: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

It happens to the best!// Don’t do this:$(window).scroll(function() { $('.foo').something();});

Page 52: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

It happens to the best!// Don’t do this:$(window).scroll(function() { $('.foo').something();});

// See http://mths.be/azs

Page 53: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

typeof performance != 'the whole story'

Page 54: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

tips & tricks(not really)

Page 55: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

New objectsvar obj = new Object();obj.x = 42;obj.y = 'foo';obj.z = false;

Page 56: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

New objectsvar obj = { 'x': 42, 'y': 'foo', 'z': false};

Page 57: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

New arraysvar arr = new Array();arr.push(42);arr.push('foo');arr.push(false);

Page 58: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

New arraysvar arr = [ 42, 'foo', false];

Page 59: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Avoid switchswitch(foo) { case 'alpha': // do X break; case 'beta': // do Y break; default: // do Z break;}

Page 60: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Avoid switchvar switchObj = { 'alpha': function() { // do X }, 'beta': function() { // do Y }, '_default': function() { // do Z }};(switchObj.hasOwnProperty(foo) && switchObj[foo] || switchObj._default)(args);

Page 61: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Don’t use jQuery for everything$('.foo').click(function() { $(this).prop('id'); // same as this, before jQuery 1.6: // $(this).attr('id');

// also `href`, `checked`, `value`…});

Page 62: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Don’t use jQuery for everything$('.foo').click(function() { this.id; this.href; this.checked; this.value; // etc.});

Page 63: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery document ready$(document).ready(function() { // teh coads});

Page 64: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery document ready$().ready(function() { // heh});

Page 65: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery document ready$.fn.ready(function() { // not pretty, but fastest solution});

Page 66: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery document ready$(function() { // moar sexy, but slower});

Page 67: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery document ready(function() { // move <script>s to the bottom // and just use an IIFE*}());

// * unless you use .appendChild() / .innerHTML on document.documentElement or document.body: http://mths.be/ieoa

Page 68: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery collection size$('.foo').size(); // NO.

Page 69: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

jQuery collection size// jQuery source:$.fn.size = function() { return this.length;};

// …so, just use:$('.foo').length;

Page 70: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Use context$('#foo .bar').addClass('baz');$('#foo .qux').hide();$('#foo input').removeClass('wut');

Page 71: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Use contextvar $foo = $('#foo');$('.bar', $foo).addClass('baz');$('.qux', $foo).hide();$('input', $foo).removeClass('wut');

Page 72: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

this.location = 'http://jsperf.com/'

Page 74: How DRY impacts JavaScript performance // Faster JavaScript execution for the lazy developer

Questions?@mathias