51
JavaScript Performance MythBusters™ (via jsPerf) SXSW 2012

SXSW 2012 JavaScript MythBusters

Embed Size (px)

Citation preview

Page 1: SXSW 2012 JavaScript MythBusters

JavaScript Performance MythBusters™ (via jsPerf)

SXSW 2012

Page 2: SXSW 2012 JavaScript MythBusters

about:me

Lindsey [email protected] / @elsigh

• AdWords• App Engine• i18n• Closure library• Search• Google Translate• Google+

Page 3: SXSW 2012 JavaScript MythBusters

about:me

Kyle [email protected] / @getify / http://getify.me• LABjs• HTML5 Cookbook• HubAustin Coworking• ATX Web Performance Meetup• Web Performance Summit (online!) • "Always bet on JS & HTML5"

Page 4: SXSW 2012 JavaScript MythBusters

about:me

John-David [email protected] / @jdalton• Benchmark.js• jsPerf.com• FuseJS• All-around JS Fanboy

Page 5: SXSW 2012 JavaScript MythBusters

about:me

Chris [email protected] / @robodynamo• Rocket Loader• CloudFlareJS

Page 6: SXSW 2012 JavaScript MythBusters
Page 7: SXSW 2012 JavaScript MythBusters

Your Development Test Environment

Page 8: SXSW 2012 JavaScript MythBusters

Your Mobile Test Environment

Page 9: SXSW 2012 JavaScript MythBusters

en.wikipedia.org/wiki/Usage_share_of_web_browsers

Page 10: SXSW 2012 JavaScript MythBusters

Browserscope & jsPerf

Open-source, community-driven project for profiling browsers.

Explicit Goals:• foster innovation by tracking functionality• push browser innovation, uncover regressions• historical resource for web developers

Page 11: SXSW 2012 JavaScript MythBusters

crowdsourcing the results

Page 12: SXSW 2012 JavaScript MythBusters

crowdsourcing

• no dedicated test resources 

Page 13: SXSW 2012 JavaScript MythBusters

• no dedicated test resources• project runs in perpetuity

 

crowdsourcing

Page 14: SXSW 2012 JavaScript MythBusters

crowdsourcing

• no dedicated test resources• project runs in perpetuity• real world test conditions

 

Page 15: SXSW 2012 JavaScript MythBusters

crowdsourcing

• no dedicated test resources• project runs in perpetuity• real world test conditions • aggregating results reduces bias

 

Page 16: SXSW 2012 JavaScript MythBusters

crowdsourcing

• no dedicated test resources• project runs in perpetuity• real world test conditions • aggregating results reduces bias • new browsers show up immediately

 

Page 17: SXSW 2012 JavaScript MythBusters

Myth:Your for loops suck: Rewrite all your code and use while --i. 

Status: Plausible/Busted(Mobile)

PS - String#concat vs. Array#join

Page 18: SXSW 2012 JavaScript MythBusters

jsperf.com/for-loop-research

jsperf.com/for-loop-caching

Page 19: SXSW 2012 JavaScript MythBusters

Myth:"Double your localStorage read performance in Chrome using indexes ls.getItem[i] not keys ls.getItem('key')"See: twitter.com/johnallsopp/status/177964915632513024

Status: Confirmed

Page 20: SXSW 2012 JavaScript MythBusters

jsperf.com/localstorage-science

jsbin.com/ifucuc/9

Page 21: SXSW 2012 JavaScript MythBusters

Myth:The arguments object should be avoided because accessing it is slow.

Status: Busted

Page 22: SXSW 2012 JavaScript MythBusters

jsperf.com/arguments-perf

jspatterns.com/arguments-considered-harmful/

Page 23: SXSW 2012 JavaScript MythBusters

Myth:Frameworks/libraries, like jQuery, are always better at managing performance than you are, so just use what they provide.

Status: Busted

Page 24: SXSW 2012 JavaScript MythBusters

http://4vk.geti.fi/jsperf/jquery-this

Page 25: SXSW 2012 JavaScript MythBusters

Myth:Converting a NodeList to an array is expensive, so you shouldn't do it.

Status: Busted

Page 26: SXSW 2012 JavaScript MythBusters

jsperf.com/lists-of-nodes

Page 27: SXSW 2012 JavaScript MythBusters

Myth:Script concatenation and/or

<script defer> is all you need to load JS performantly.

(aka "Issue 28")github.com/h5bp/html5-boilerplate/issues/28

Status: (Mostly) Busted

Page 28: SXSW 2012 JavaScript MythBusters

<script defer>horribly buggy across browsers (esp IE<=9!)github.com/paulirish/lazyweb-requests/issues/42 

still would delay the DOM-ready event (bad!)...and btw, this is also buggy cross-browser!

Page 29: SXSW 2012 JavaScript MythBusters

<script> or <script defer>

dynamic script loading

Page 30: SXSW 2012 JavaScript MythBusters

<script "one-big-file.js">at some point, "big" is big enough to overcome HTTP connection overhead, meaning a second parallel request for half the file will be faster.

NOTE: do concat your files first, then (if size >=100k) chunk into 2-3 ~equal-sized parts.

Page 31: SXSW 2012 JavaScript MythBusters

<script "one-big-file.js">chunking a big file into 2-3 parts, based on volatility of code (how often it changes) allows for different caching headers for each part (win!).

NOTE: jquery.js is far more stable than your site's code.

Page 32: SXSW 2012 JavaScript MythBusters

<script "one-big-file.js">chunking your concat'd code file allows you to load the chunks separately, like one before load, the next 100ms after DOM-ready, etc.

NOTE: lazy-loading is an established best-practice!

Page 33: SXSW 2012 JavaScript MythBusters

* <script defer> only works in IE10+

Experimenting with JS loading techniques on a real site (incl. jquery, jquery-ui, site code)

Page 34: SXSW 2012 JavaScript MythBusters

Myth:"eval is evil", or in other words, eval is too slow and quirky to be considered useful.

Status: Plausible

Page 35: SXSW 2012 JavaScript MythBusters

jsperf.com/practical-eval/2

jsperf.com/more-practical-eval/3

jsperf.com/eval-kills

Page 36: SXSW 2012 JavaScript MythBusters

Myth:Regular expressions are slow and should be replaced with simple string method calls, like indexOf, when possible.

Status: Busted

Page 37: SXSW 2012 JavaScript MythBusters

jsperf.com/regexp-indexof-perf

es5.github.com/#x7.8.5

Page 38: SXSW 2012 JavaScript MythBusters

Myth:OO API abstraction means you never have to worry about the details (including the performance).

Status: Busted

Page 39: SXSW 2012 JavaScript MythBusters

http://nam.geti.fi/jsperf/ooapi

Page 40: SXSW 2012 JavaScript MythBusters

Myth:Caching "nested property lookup" or "prototype chain lookup" helps (or hurts) performance.

Status: Busted

Page 41: SXSW 2012 JavaScript MythBusters

http://pc6.geti.fi/jsperf/prototype-chain

http://q3y.geti.fi/jsperf/nested-properties

Page 42: SXSW 2012 JavaScript MythBusters

Myth:Operations which require an implicit primitive-to-object cast/conversion will be slower.

Status: Busted

Page 43: SXSW 2012 JavaScript MythBusters

http://udw.geti.fi/jsperf/object-casting

Page 44: SXSW 2012 JavaScript MythBusters

Myth:Scope chain lookups are expensive.

Status: Busted

Page 45: SXSW 2012 JavaScript MythBusters

jsperf.com/scope-chain-perf

Page 46: SXSW 2012 JavaScript MythBusters

Myth:Use void 0 instead of undefined and === instead of == when concerned about performance.

Status: Busted

Page 47: SXSW 2012 JavaScript MythBusters

jsperf.com/double-vs-triple-equals

es5.github.com/#x11.9.3

es5.github.com/#x11.9.6

jsperf.com/undefined-void0-perf

Page 48: SXSW 2012 JavaScript MythBusters

Myth:Use switch statements instead of if / else if for better performance.

Status: Plausible/Busted(WebKit Mobile)

Page 49: SXSW 2012 JavaScript MythBusters

jsperf.com/switch-if-else

Page 50: SXSW 2012 JavaScript MythBusters

Myth:Use native methods for better performance.

Status: Busted

Page 51: SXSW 2012 JavaScript MythBusters

jsperf.com/for-vs-array-foreach

es5.github.com/#x15.4.4.18

jsperf.com/accessor-perf

jsperf.com/bind-vs-custom