John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE...

101

Transcript of John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE...

Page 1: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.
Page 2: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Improving…

John Lawrimore, 2014

JavaScript Performance

Page 3: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Presenter

JOHN [email protected]@johnlawrimore

- Principal Consultant at Improving Enterprises- Microsoft enthusiast- Background heavy in web applications development- Unable to think of interesting things to say about self

Page 4: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Access this Presentation Online http://johnlawrimore.com/jsperformance

Page 5: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Objectives1. Provide a better understanding of how JavaScript works2. Introduce practices and techniques that will noticeably increase

performance3. Focus ONLY on what...

• Generally applies to all browsers• You can reasonably begin implementing tomorrow

Page 6: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Approaching Optimization• The right optimization strategy will differ by application• Do what makes sense• Pick your battles• Consider maintainability• Challenge your assumptions regularly

Page 7: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What is scope chain?

Page 8: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Scope Chain

Page 9: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.
Page 10: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Namespacing• Condenses the global namespace• Reduces search time for its descendants

HINT: Set distant namespaces to a variable to reduce search time

Page 11: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Closures (anonymous functions)Pros:• Access to variables outside the function

Cons:• Adds to the scope chain• Requires a new execution context

Page 12: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong with this?

Page 13: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong with this?

Page 14: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

With and Try-Catch• Use Try-Catch sparingly• And NEVER try catch in a loop!

• NEVER use With

!

Page 15: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s missing?

Page 16: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Local variables missing the var keyword are automatically instantiated in the global namespace!!!

What’s missing?

Page 17: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Managing Scope• Don’t use the global namespace!• Cache out-of-scope variables referenced more than once• Limit augmentations to the scope chain• Closures• With• Try Catch

• Consider scope when creating prototypes• Don’t forget to use ‘var’

Page 18: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What is the DOM?

Page 19: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Document Object Model

Page 20: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Under the hood

Page 21: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What impacts DOM performance?

Page 22: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

DOM Performance Issues

DOM search

DOM manipulation

!

!

Page 23: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong with this?

Page 24: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

THE DOM IS NOT A DATABASE!!

What’s wrong with this?

Page 25: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Data Property• Maintained within the JavaScript engine (not the DOM)• Can be global or bound to a specific element

Page 26: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Data vs. Attributes

Attribute Storage Data PropertyAvoids hits to the DOM

Accommodates types, arrays and complex objects

Optimized by built-in caching

Maintains valid HTML

Page 27: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Data vs. Attributes

Page 28: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What is Reflow/Repaint?

Page 29: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Repaint vs. ReflowREPAINTVisual changes that do not alter the document layout. (aka: redraw)

Examples:• Color• Font style

REFLOWOccurs when a visual change is made to the HTML document that requires layout to be computed.

Examples:• Initial render• Browser window resize• Visible elements added or removed• Element position changes• Changes in size, margin, border, etc.• Text changes• Layout information is retrieved (in some cases)

Page 30: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Repaint vs. Reflow

• Both are expensive in terms of performance because they require traversing• Reflow may or may not affect the entire document• JavaScript engines attempt to perform reflow in batches

Page 31: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 32: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 33: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 34: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 35: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

DocumentFragment• Child of the document that created it• No visual representation• Construct happens behind the scenes• When passed to appended to document, only its children are added

Page 36: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

CSS Transitions > Animation• Allows property changes in CSS values to occur smoothly over a

specified duration.• Powerful, clean, and faster than JS animations• Not supported in IE 9 and earlier

Page 37: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Avoiding Reflow• Group styles whenever possible

• Use classes or cssText instead of styles

• Accumulate new elements and append with one call• innerHTML (or html() for jQuery) • documentFragment

• Temporarily remove elements to be modified from the document• remove(), detach(), etc.

• Avoid animation, and leverage libraries or CSS transitions when you must

Page 38: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What is the outcome of this method?

Page 39: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What is the outcome of this method?

IT NEVER ENDS!!

Page 40: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

HtmlCollectionsDon’t let them fool you! They are not arrays!

Examples:• document.getElementsByTagName• document.getElemenetsByClassName• document.images• document.forms

Page 41: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 42: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Only touch the HTMLCollection one time

Page 43: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Reducing DOM InteractionNever store data on the DOM

Avoid reflow

Limit calls to HtmlCollections

Page 44: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

UI ThreadResponsibilities• Draw UI• Execute JavaScript

! UI THREAD CAN ONLY DO ONE RESPONSIBILITY AT A TIME

Page 45: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Timers• Run secondary tasks in the back ground AFTER the page has loaded.• When timer is done, job is added to the UI Queue.• Remember that less timers with more work is better than more timers

with less work.

Page 46: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Web Workers• New option for asynchronous execution in HTML5• Operates outside of UI Thread• Trigger specified event handlers when completed

Page 47: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What loop should I use?

Page 48: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Loopsfor

for-in

for-each

[].forEach()

$().each()

do-while

while

Page 49: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Selecting a LoopAvoid non-native function based loops• Creates closures (and associated overhead) in scope chain• Takes about 8-10x as long as basic for loop

Avoid for-in and for-each• for-in performs expensive type evaluation• for-each deprecated in ECMA -357

Page 50: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Selecting a LoopWhat matters?

• Amount of work being performed• Number of iterations

Do Less Work!!

Avoid lookups inside loop

Reduce number of calculation being performed

Page 51: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

How can this be improved?

Page 52: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Combine control condition and control variable into a single statement

How can this be improved?

BET TER!

Page 53: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Combine the terminal condition evaluation w/ incrementing / decrementing

How can this be improved?

BET TER!

BEST!

Page 54: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Switch or If-Then-Else?

Page 55: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Selecting a Conditional StatementIt Depends!• As conditions increase, switch becomes more and more optimal

Page 56: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 57: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

BET TER!Consider a combined approach!

Page 58: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

BET TER!

Create a hash table

OR EVEN

Page 59: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Common Pitfalls

Page 60: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

Page 61: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

Page 62: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

MEMORY LEAK!!

Page 63: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

MEMORY LEAK!!

Break the chain!

Page 64: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

Page 65: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What’s wrong here?

WILL BE CALLED WAY TOO MUCH!!

Page 66: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Throttle and Debounce• Throttling limits the execution of a function to no more than once

every delay milliseconds• Debouncing guarantees that the function will only ever be executed a

single time (given a specified threshold).

Page 67: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Smarter Coding

Page 68: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which is better?

Page 69: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which is better?

BET TER!

Use literals, avoid ‘new’

Page 70: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which way is better?

Page 71: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which way is better?

Use prototypes!

Page 72: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Reuse/Recycle

Reuse old variables instead of creating new ones!

Page 73: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Event DelegationHandle array element events with a single binding on the container• Events to bubble up the DOM tree• Detect the node originating the event and act accordingly• Use stopPropagation(), preventDefaults(), etc. as needed

Page 74: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

Page 75: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s improve this!

BET TER!

Use lazy loading

Page 76: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

String Manipulation Use += over function based concatenation

Use regexes to parse and slice

Page 77: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which is faster?

Page 78: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Which is faster?

BET TER!

Use bitwise operators where appropriate

Page 79: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Traversing Always use the most optimum methods for crawling the DOM

• DOM properties such as childNode, firstChild, nextSibling, etc. don’t distinguish between element and other node types (such as spaces, comments, etc.)

Page 80: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

JSONeval() SLOW!Libraries EVEN WORSE!

Always use the JSON namespace• Native in all modern browsers (IE8+)• JSON.parse()• JSON.stringify()• toJSON() prototype

!

Page 81: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Are libraries efficient?

Page 82: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Using LibrariesThe most optimal library is no match to the JavaScript Engine!• Intermediaries come with overhead• Native methods are compiled machine code

Leverage libraries only in cases where value is added…

Page 84: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

querySelectorAll()Much faster than using JavaScript and DOM to iterate and narrow down a list of elements• Supported in IE8+

Page 85: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

What selector should I use?

Page 86: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

SelectorsAll selectors are NOT created equal

Always consider what is going on underthe covers!

Type ExampleUniversal *

Type div

ID #header

Class .promo

Attribute [type=“text”]

Pseudo-class a:hover

Adjacent sibling h2 + p

Child li > ul

Descendant ul a

Page 87: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

ID & Element Selectors$(‘#Element, form, input’)

Most optimal choice!

Backed by native DOM operations• eg. getElementById()

Page 88: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Class Selectors$(‘.element’)

Much slower selector• Particularly in browsers where getElementsByClassName() is not supported

!

Page 89: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Pseudo & Attribute$(‘:visible, :hidden’);$(‘[attribute=value]’);

USE SPARINGLY

No native calls available• querySelector() and querySelectorAll() help in modern browsers

!

Page 90: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Combining SelectorsSelector engines use the Right to Left Model

Always make your MOST SPECIFIC selector on the right

Page 91: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s rank these!

A. $('.child', $('#parent')) B. $(‘#parent > .child’)

C. $parent.find(‘.child’) D. $(‘#parent .child’)

E. $(‘.child’, $parent) F. $parent.children(".child’)

Page 92: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Let’s rank these!

D. $(‘#parent .child’)

B. $(‘#parent > .child’)

F. $parent.children(".child’)

A. $('.child', $('#parent'))

E. $(‘.child’, $parent)

C. $parent.find(‘.child’)

RESULTS:

Page 93: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Can Script Tags Hurt Performance?

Page 94: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Script TagsPROBLEMS• External references result in an http request• Inline scripts circumvent client-side caching

Page 95: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Load Script Tags FasterALWAYS "minify" production code!

Bundle scripts (and styles) into single resources

Leverage Code Delivery Networks (CDN)

Lazy load scripts with AMD utilities

Page 96: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

AMDAsynchronous Module Definition

Example: require.js

Page 97: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

How do I Test Performance?

Page 98: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

jsPerfCompare the performance of code snippets across browsers

Page 99: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

YSlowGrade your site’s optimizations

Page 100: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Reading Material

High Performance JavaScriptNicholas C. Zakas

Page 101: John Lawrimore, 2014 JavaScript Performance Presenter JOHN LAWRIMORE john.lawrimore@improvingenterprises.com @johnlawrimore - Principal Consultant at.

Questions?

[email protected]

htt p://linkedin.com/in/johnlawrimore