high performance javascript

132
HIGH PERFORMANCE JAVASCRIPT by Mike Wilcox Wednesday, September 3, 2014

Upload: mike-wilcox

Post on 31-Oct-2014

200 views

Category:

Technology


4 download

DESCRIPTION

Performance is a big topic, but this talk will at least touch on the areas that tend to slow down your application: Resource loading, libraries, DOM, CSS, memory, and the application itself. Then we will focus on getting maximum performance from JavaScript and your applications.

TRANSCRIPT

Page 1: High Performance JavaScript

HIGH PERFORMANCE JAVASCRIPT

by Mike Wilcox

Wednesday, September 3, 2014

Page 2: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Before tuning the JavaScript...

Wednesday, September 3, 2014

Page 3: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Wednesday, September 3, 2014

Page 4: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Wednesday, September 3, 2014

Page 5: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Wednesday, September 3, 2014

Page 6: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Application Architecture

Wednesday, September 3, 2014

Page 7: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Application Architecture

DOM

Wednesday, September 3, 2014

Page 8: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Application Architecture

DOM

CSS

Wednesday, September 3, 2014

Page 9: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Application Architecture

DOM

CSS

Memory Management

Wednesday, September 3, 2014

Page 10: High Performance JavaScript

WHAT NEEDS PERFORMANCE

Page and Resource Loading

Libraries or Library Usage

Application Architecture

DOM

CSS

Memory Management

JavaScript

Wednesday, September 3, 2014

Page 11: High Performance JavaScript

PAGE AND RESOURCE LOADING

Wednesday, September 3, 2014

Page 12: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Wednesday, September 3, 2014

Page 13: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

Wednesday, September 3, 2014

Page 14: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

Wednesday, September 3, 2014

Page 15: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Wednesday, September 3, 2014

Page 16: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Too few resources can be very slow

Wednesday, September 3, 2014

Page 17: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Too few resources can be very slow

Not uncommon for web apps to have 3+ megabytes of code

Wednesday, September 3, 2014

Page 18: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Too few resources can be very slow

Not uncommon for web apps to have 3+ megabytes of code

Solution:

Wednesday, September 3, 2014

Page 19: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Too few resources can be very slow

Not uncommon for web apps to have 3+ megabytes of code

Solution:

Load a main core file, enough to display the page, then lazy-load the rest, or as needed.

Wednesday, September 3, 2014

Page 20: High Performance JavaScript

PAGE AND RESOURCE LOADING

Too many resources is very slow

Using requirejs in dev mode can mean 50 files

DNS lookup + only a few files load in parallel

The 50 jQuery plugin scripts is a no-no

Too few resources can be very slow

Not uncommon for web apps to have 3+ megabytes of code

Solution:

Load a main core file, enough to display the page, then lazy-load the rest, or as needed.

Learn (much) more: http://www.slideshare.net/anm8tr/faster-websites-kill-kill-4402077

Wednesday, September 3, 2014

Page 21: High Performance JavaScript

LIBRARIES

Wednesday, September 3, 2014

Page 22: High Performance JavaScript

LIBRARIES

Use the right library

Wednesday, September 3, 2014

Page 23: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Wednesday, September 3, 2014

Page 24: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Wednesday, September 3, 2014

Page 25: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

Wednesday, September 3, 2014

Page 26: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

The Dojo fallacy was making “everything a widget”

Wednesday, September 3, 2014

Page 27: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

The Dojo fallacy was making “everything a widget”

Angular makes everything a component

Wednesday, September 3, 2014

Page 28: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

The Dojo fallacy was making “everything a widget”

Angular makes everything a component

Knockout templates can get slow

Wednesday, September 3, 2014

Page 29: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

The Dojo fallacy was making “everything a widget”

Angular makes everything a component

Knockout templates can get slow

No library can be the right library

Wednesday, September 3, 2014

Page 30: High Performance JavaScript

LIBRARIES

Use the right library

jQuery is easy not fast

Angular needs to load a lot of boilerplate and polyfills

Use the library the way it was intended

The Dojo fallacy was making “everything a widget”

Angular makes everything a component

Knockout templates can get slow

No library can be the right library

No overhead to download - write exactly what the app needsWednesday, September 3, 2014

Page 31: High Performance JavaScript

APPLICATION ARCHITECTURE

Wednesday, September 3, 2014

Page 32: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Wednesday, September 3, 2014

Page 33: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Wednesday, September 3, 2014

Page 34: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Display *something* as soon as possible

Wednesday, September 3, 2014

Page 35: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Display *something* as soon as possible

Know the limits of:

Wednesday, September 3, 2014

Page 36: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Display *something* as soon as possible

Know the limits of:

The browser

Wednesday, September 3, 2014

Page 37: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Display *something* as soon as possible

Know the limits of:

The browser

Your users

Wednesday, September 3, 2014

Page 38: High Performance JavaScript

APPLICATION ARCHITECTURE

Don’t parse everything at once

Don’t download all the data at once

Display *something* as soon as possible

Know the limits of:

The browser

Your users

You

Wednesday, September 3, 2014

Page 39: High Performance JavaScript

DOM

Wednesday, September 3, 2014

Page 40: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

Wednesday, September 3, 2014

Page 41: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Wednesday, September 3, 2014

Page 42: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Wednesday, September 3, 2014

Page 43: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Image resizing is extra work for the browser

Wednesday, September 3, 2014

Page 44: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Image resizing is extra work for the browser

Like in CSS, setting and getting sizes and positions, and scroll causes a reflow

Wednesday, September 3, 2014

Page 45: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Image resizing is extra work for the browser

Like in CSS, setting and getting sizes and positions, and scroll causes a reflow

addEventListener is not free

Wednesday, September 3, 2014

Page 46: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Image resizing is extra work for the browser

Like in CSS, setting and getting sizes and positions, and scroll causes a reflow

addEventListener is not free

Add listeners to the parent and check the clicked node via event.target

Wednesday, September 3, 2014

Page 47: High Performance JavaScript

DOM

Most of the time, creating nodes is faster than a big innerHTML

createTextNode can be many times faster than innerHTML

Create your widget in a fragment first, then add it to the page

Image resizing is extra work for the browser

Like in CSS, setting and getting sizes and positions, and scroll causes a reflow

addEventListener is not free

Add listeners to the parent and check the clicked node via event.target

Do NOT add a listener to hundreds of child nodes

Wednesday, September 3, 2014

Page 48: High Performance JavaScript

CSS

Wednesday, September 3, 2014

Page 49: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Wednesday, September 3, 2014

Page 50: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Wednesday, September 3, 2014

Page 51: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Wednesday, September 3, 2014

Page 52: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Use element.style as little as possible

Wednesday, September 3, 2014

Page 53: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Use element.style as little as possible

Set classNames (or better, use classList)

Wednesday, September 3, 2014

Page 54: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Use element.style as little as possible

Set classNames (or better, use classList)

Use transitions or animations, not jQuery

Wednesday, September 3, 2014

Page 55: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Use element.style as little as possible

Set classNames (or better, use classList)

Use transitions or animations, not jQuery

Know what styles affect a page reflow and affect performance

Wednesday, September 3, 2014

Page 56: High Performance JavaScript

CSS

Use as few stylesheets as reasonable

Keep them clean and maintained, so time is wasted downloading unused definitions

Don’t use JavaScript when it’s a job for CSS

Use element.style as little as possible

Set classNames (or better, use classList)

Use transitions or animations, not jQuery

Know what styles affect a page reflow and affect performance

Namely anything to do with sizes: setting as well as getting

Wednesday, September 3, 2014

Page 57: High Performance JavaScript

MEMORY MANAGEMENT

Value Graph

Root Node Object Node

Scalar Node

How Garbage Collection Works

Wednesday, September 3, 2014

Page 58: High Performance JavaScript

MEMORY MANAGEMENTObject is dereferenced, and it and its descendants are slated for GC

Value GraphWednesday, September 3, 2014

Page 59: High Performance JavaScript

MEMORY MANAGEMENTHowever, if there is a reference to any object in the tree, it can’t be released

Value GraphWednesday, September 3, 2014

Page 60: High Performance JavaScript

MEMORY MANAGEMENTValue graph example shown in code

var app = {! registry: {},! getWidget: function(id){! ! var widget = {};! ! this.registry[id] = widget;! ! return widget;! }};var widget = app.getWidget('mine');widget = null; // will not release

Wednesday, September 3, 2014

Page 61: High Performance JavaScript

MEMORY MANAGEMENTValue graph example shown in code

var app = {! registry: {},! getWidget: function(id){! ! var widget = {};! ! this.registry[id] = widget;! ! return widget;! }};var widget = app.getWidget('mine');widget = null; // will not release

Moral: Clean up after yourself. Suggest a dispose(); method on the widget that removes itself from the registry

Wednesday, September 3, 2014

Page 62: High Performance JavaScript

Before tuning the JavaScript...

WHEN TO OPTIMIZE

Wednesday, September 3, 2014

Page 63: High Performance JavaScript

Wednesday, September 3, 2014

Page 64: High Performance JavaScript

“Premature optimization is the root of all evil”

~ Donald Knuth

Wednesday, September 3, 2014

Page 65: High Performance JavaScript

“Premature optimization is the root of all evil”

~ Donald Knuth

“Except when you should be planning ahead.”

~ Mike Wilcox

Wednesday, September 3, 2014

Page 66: High Performance JavaScript

PLANNING AHEAD

Wednesday, September 3, 2014

Page 67: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Wednesday, September 3, 2014

Page 68: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Wednesday, September 3, 2014

Page 69: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Wednesday, September 3, 2014

Page 70: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Careful with scope

Wednesday, September 3, 2014

Page 71: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Careful with scope

Browsers are actually really good at GC

Wednesday, September 3, 2014

Page 72: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Careful with scope

Browsers are actually really good at GC

But know when you are capturing in your closures

Wednesday, September 3, 2014

Page 73: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Careful with scope

Browsers are actually really good at GC

But know when you are capturing in your closures

Use JSHint in your IDE

Wednesday, September 3, 2014

Page 74: High Performance JavaScript

PLANNING AHEAD

Clean up after yourself

Implement or use a widget life cycle system

Unbind event listeners that aren't needed any more

Careful with scope

Browsers are actually really good at GC

But know when you are capturing in your closures

Use JSHint in your IDE

It will warn against bad practices that can slow your code

Wednesday, September 3, 2014

Page 75: High Performance JavaScript

DON’T WORRY

Wednesday, September 3, 2014

Page 76: High Performance JavaScript

DON’T WORRY

Maintainability trumps performance 98.6% of the time

Wednesday, September 3, 2014

Page 77: High Performance JavaScript

DON’T WORRY

Maintainability trumps performance 98.6% of the time

90% of the time, the only performance you'll need is rendering DOM

Wednesday, September 3, 2014

Page 78: High Performance JavaScript

DON’T WORRY

Maintainability trumps performance 98.6% of the time

90% of the time, the only performance you'll need is rendering DOM

Code that is async, or immediately after is not performance critical

Wednesday, September 3, 2014

Page 79: High Performance JavaScript

DON’T WORRY

Maintainability trumps performance 98.6% of the time

90% of the time, the only performance you'll need is rendering DOM

Code that is async, or immediately after is not performance critical

Size matters

Wednesday, September 3, 2014

Page 80: High Performance JavaScript

DON’T WORRY

Maintainability trumps performance 98.6% of the time

90% of the time, the only performance you'll need is rendering DOM

Code that is async, or immediately after is not performance critical

Size matters

A loop of 20 items will be fine in a forEach, suffering a few measly milliseconds

Wednesday, September 3, 2014

Page 81: High Performance JavaScript

CRITICAL AREAS

Wednesday, September 3, 2014

Page 82: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Wednesday, September 3, 2014

Page 83: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Template parsing; especially many widgets on page load

Wednesday, September 3, 2014

Page 84: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Template parsing; especially many widgets on page load

Canvas rendering; especially animation

Wednesday, September 3, 2014

Page 85: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Template parsing; especially many widgets on page load

Canvas rendering; especially animation

Oft-triggered actions, like on page resize or mousemove

Wednesday, September 3, 2014

Page 86: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Template parsing; especially many widgets on page load

Canvas rendering; especially animation

Oft-triggered actions, like on page resize or mousemove

Millions of items of grid data

Wednesday, September 3, 2014

Page 87: High Performance JavaScript

CRITICAL AREAS

Memory is always critical - it can give you the scrolling “janks”

Template parsing; especially many widgets on page load

Canvas rendering; especially animation

Oft-triggered actions, like on page resize or mousemove

Millions of items of grid data

Just kidding. You should never do this.

Wednesday, September 3, 2014

Page 88: High Performance JavaScript

JAVASCRIPT PERFORMANCE

Now you’re ready improve your...

Wednesday, September 3, 2014

Page 89: High Performance JavaScript

AVOID EVAL

Wednesday, September 3, 2014

Page 90: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Wednesday, September 3, 2014

Page 91: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Expensive operations as each time they are called, the script engine must convert source to executable code

Wednesday, September 3, 2014

Page 92: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Expensive operations as each time they are called, the script engine must convert source to executable code

Has to be interpreted at runtime - #NOJIT

Wednesday, September 3, 2014

Page 93: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Expensive operations as each time they are called, the script engine must convert source to executable code

Has to be interpreted at runtime - #NOJIT

Better:

Wednesday, September 3, 2014

Page 94: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Expensive operations as each time they are called, the script engine must convert source to executable code

Has to be interpreted at runtime - #NOJIT

Better:

Use JSON

Wednesday, September 3, 2014

Page 95: High Performance JavaScript

AVOID EVAL

As well as new Function(...) or a string in setTimeout()

Expensive operations as each time they are called, the script engine must convert source to executable code

Has to be interpreted at runtime - #NOJIT

Better:

Use JSON

Parse function with a predetermined convention

Wednesday, September 3, 2014

Page 96: High Performance JavaScript

Slow:with (test.object) { foo = 'Value of foo property of object'; bar = 'Value of bar property of object';}

Faster:var myObj = test.object;myObj.foo = 'Value of foo property of object';myObj.bar = 'Value of bar property of object';

Hopefully you weren’t doing this...

AVOID WITH

Wednesday, September 3, 2014

Page 97: High Performance JavaScript

Slow:with (test.object) { foo = 'Value of foo property of object'; bar = 'Value of bar property of object';}

Faster:var myObj = test.object;myObj.foo = 'Value of foo property of object';myObj.bar = 'Value of bar property of object';

Hopefully you weren’t doing this...

AVOID WITH

Wednesday, September 3, 2014

Page 98: High Performance JavaScript

Slow: for (i = 0; i < object.length; i++) { try { // do something that throws an exception } catch (e) { // handle exception }}

Faster:try { for (i = 0; i < object.length; i++) { // do something }} catch (e) { // handle exception}

#NOJIT

TRY-CATCH

Wednesday, September 3, 2014

Page 99: High Performance JavaScript

Slow: for (i = 0; i < object.length; i++) { try { // do something that throws an exception } catch (e) { // handle exception }}

Faster:try { for (i = 0; i < object.length; i++) { // do something }} catch (e) { // handle exception}

#NOJIT

TRY-CATCH

Wednesday, September 3, 2014

Page 100: High Performance JavaScript

Slow:window.str = '';function globalScope() { for (var i=0; i < 100; i++) { str += i; }}

Faster:window.str = '';function localScope() { var tempstr = ''; for (var i=0; i < 100; i++) { tempstr += i; } str = tempstr;}

Slow lookup; Bad GC

AVOID GLOBAL SCOPE

Wednesday, September 3, 2014

Page 101: High Performance JavaScript

Slow:window.str = '';function globalScope() { for (var i=0; i < 100; i++) { str += i; }}

Faster:window.str = '';function localScope() { var tempstr = ''; for (var i=0; i < 100; i++) { tempstr += i; } str = tempstr;}

Slow lookup; Bad GC

AVOID GLOBAL SCOPE

Wednesday, September 3, 2014

Page 102: High Performance JavaScript

// always the same! Get the variable firstvar a = 0;for(var i = 0; i < array.length; i++){

a += Math.sin(1);}

// creating a new function every time #NOJITvar a = 0, b = [1,2,3];for(var i = 0; i < array.length; i++){

b.forEach(function(num){a += num;

}); }

Functions take time, creating them is worse

UNNECESSARY FUNCTIONS

Wednesday, September 3, 2014

Page 103: High Performance JavaScript

// always the same! Get the variable firstvar a = 0;for(var i = 0; i < array.length; i++){

a += Math.sin(1);}

// creating a new function every time #NOJITvar a = 0, b = [1,2,3];for(var i = 0; i < array.length; i++){

b.forEach(function(num){a += num;

}); }

Functions take time, creating them is worse

UNNECESSARY FUNCTIONS

Wednesday, September 3, 2014

Page 104: High Performance JavaScript

// always the same! Get the variable firstvar a = 0;for(var i = 0; i < array.length; i++){

a += Math.sin(1);}

// creating a new function every time #NOJITvar a = 0, b = [1,2,3];for(var i = 0; i < array.length; i++){

b.forEach(function(num){a += num;

}); }

Functions take time, creating them is worse

UNNECESSARY FUNCTIONS

Wednesday, September 3, 2014

Page 105: High Performance JavaScript

// Problem: for large arrays, this can get slowvar items = [{id:'a'}, {id:'b'}, {id:'c'}];function getItemById(id){! for(var i = 0; i < items.length; i++){! ! if(items[i].id === id){! ! ! return items[i];! ! }! }}

// Solution: use a hash map insteadvar items = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};function getItemById(id){! return items[id];}

Unnecessary Array lookups

LIST VS MAPS

Wednesday, September 3, 2014

Page 106: High Performance JavaScript

// Problem: for large arrays, this can get slowvar items = [{id:'a'}, {id:'b'}, {id:'c'}];function getItemById(id){! for(var i = 0; i < items.length; i++){! ! if(items[i].id === id){! ! ! return items[i];! ! }! }}

// Solution: use a hash map insteadvar items = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};function getItemById(id){! return items[id];}

Unnecessary Array lookups

LIST VS MAPS

Wednesday, September 3, 2014

Page 107: High Performance JavaScript

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 108: High Performance JavaScript

// Solution: Why not use both?

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 109: High Performance JavaScript

// Solution: Why not use both?var

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 110: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 111: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 112: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 113: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 114: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 115: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 116: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 117: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 118: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 119: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 120: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 121: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){! return itemMap[id];

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 122: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){! return itemMap[id];}

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 123: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){! return itemMap[id];}

// Could also get tricky:

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 124: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){! return itemMap[id];}

// Could also get tricky: itemList.map = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 125: High Performance JavaScript

// Solution: Why not use both?var! itemList = [{id:'a'}, {id:'b'}, {id:'c'}],! itemMap = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};!function addItem(item){! itemList.push(item);! itemMap[item.id] = item;}function getItemByIndex(index){! return itemList[index];}

function getItemById(id){! return itemMap[id];}

// Could also get tricky: itemList.map = {a:{id:'a'}, b:{id:'b'}, c:{id:'c'}};

Problem: you need an “ordered hash map”

LIST VS MAPS

Wednesday, September 3, 2014

Page 126: High Performance JavaScript

// Fastestfor(var i = 0; i < array.length; i++){

this.doThing(i); }

// Fast(ish)array.forEach(function(){

doThing(i); });

// Slowvar self = this;array.forEach(function(i){ self.doThing(i); });

// Slowestarray.forEach(function(i){ this.doThing(i); }, this);

Changing context on a function is costly

THIS

Wednesday, September 3, 2014

Page 127: High Performance JavaScript

OBJECTS VS ARRAYS

http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/Wednesday, September 3, 2014

Page 128: High Performance JavaScript

OBJECTS VS ARRAYS

Properties on objects are quite complex: they can be created with setters, and with differing enumerability and writability.

http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/Wednesday, September 3, 2014

Page 129: High Performance JavaScript

OBJECTS VS ARRAYS

Properties on objects are quite complex: they can be created with setters, and with differing enumerability and writability.

Items in arrays aren’t able to be customized as heavily — they either exist or they don’t. At an engine level, this allows for more optimization.

http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/Wednesday, September 3, 2014

Page 130: High Performance JavaScript

OBJECTS VS ARRAYS

Properties on objects are quite complex: they can be created with setters, and with differing enumerability and writability.

Items in arrays aren’t able to be customized as heavily — they either exist or they don’t. At an engine level, this allows for more optimization.

This is particularly beneficial when the array contains numbers. For example, when you need vectors, don’t define a class with properties x, y, z; use an array instead.

http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/Wednesday, September 3, 2014

Page 131: High Performance JavaScript

Memoryhttps://speakerdeck.com/addyosmani/javascript-memory-management-masterclass

Loopshttp://jsperf.com/fastest-array-loops-in-javascript/32

http://jsperf.com/add-a-property-to-an-array/2http://jsperf.com/array-vs-uint32array

Functionshttp://jsperf.com/iifes-vs-nested-functions/2

Performancehttps://github.com/sq/JSIL/wiki/JavaScript-Performance-For-Madmen

https://gist.github.com/khoomeister/4985691 (make js faster)http://www.html5rocks.com/en/tutorials/speed/v8/

http://www.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/http://developer.nokia.com/community/wiki/JavaScript_Performance_Best_Practices

http://code.mendhak.com/angular-performance/

Librarieshttp://xepler.com/blog/2014/03/19/the-good-the-bad-and-the-ugly-a-jquery-tale

References

Wednesday, September 3, 2014

Page 132: High Performance JavaScript

Wednesday, September 3, 2014