Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Optimising your JavaScript
Search
Wendy Liu
February 26, 2014
Programming
550
1
Share
Optimising your JavaScript
Presented at ConFoo (
http://confoo.ca/en/2014/session/optimising-your-javascript
)
Wendy Liu
February 26, 2014
More Decks by Wendy Liu
See All by Wendy Liu
Git and Github: Version control for a happier you
dellsystem
0
350
Git and Github: Tips and tricks
dellsystem
2
400
Git and Github: version control for the 21st century
dellsystem
1
320
Django: The web framework for perfectionists with deadlines
dellsystem
0
440
Git and GitHub: an introduction
dellsystem
2
270
What's in a name? Using first names as features for gender inference in Twitter
dellsystem
1
490
diva.js: A web-based document viewer for high-resolution images
dellsystem
0
370
Other Decks in Programming
See All in Programming
Structured Concurrency, Scoped Values and Joiners in the JDK 25 26 27
josepaumard
1
130
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
380
第3木曜LT会 #28
tinykitten
PRO
0
120
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
2.1k
AIと共に生きる技術選定 2026
sgash708
0
120
Surviving Black Friday: 329 billion requests with Falcon!
ioquatix
0
2.5k
Import assertionsが消えた日~ECMAScriptの仕様はどう決まり、なぜ覆るのか~
bicstone
2
170
〜バイブコーディングを超えて〜 チームで実験し続けたAI駆動開発
tigertora7571
0
180
Firefoxにコントリビューションして得られた学び
ken7253
2
150
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
190
【26新卒研修】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
120
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
120
Featured
See All Featured
Are puppies a ranking factor?
jonoalderson
1
3.4k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
270
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.7k
Faster Mobile Websites
deanohume
310
31k
Visualization
eitanlees
150
17k
How GitHub (no longer) Works
holman
316
150k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Designing Powerful Visuals for Engaging Learning
tmiket
1
360
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
200
Scaling GitHub
holman
464
140k
Prompt Engineering for Job Search
mfonobong
0
290
Transcript
optimising your javascript Wendy Liu February 26, 2014 :: ConFoo
BEFOre we begin ... Is optimisation even necessary?
IF SO ... What should you optimise?
PROFILING Chrome: bit.ly/chromeprofiler
tradeoffs Performance vs. readability
None
optimisation tips
faster code delivery Minimisation CDNs GZIP compression
event-handling Use a library Delegation
event-handling Use a library Delegation <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li>
... </ul>
unnecessary work Don't do it Check before calling Throttle event-handling
speeding up code Native functions Combine timers Avoid anything eval-like
speeding up code Native functions Combine timers Avoid anything eval-like
for (...) { element = $(...); ... }
speeding up code Native functions Combine timers Avoid anything eval-like
for (...) { element = document.getElementById(...); ... }
speeding up code Native functions Combine timers Avoid anything eval-like
setTimeout('someFunction', 1000); setTimeout(someFunction, 1000)
don't use: with try-catch in a loop globals for in
speeding up code Cache out of scope variables Cache array.length
String-building Cache calculations someFunction = someObject.someFunction; for (...) { someFunction(...); }
speeding up code Cache out of scope variables Cache array.length
String-building Cache calculations for (var i = 0, l = arr.length; i < l; i++) { ... }
speeding up code Cache out of scope variables Cache array.length
String-building Cache calculations var stringBuilder = []; for (...) { stringBuilder.push('a'); } var string = stringBuilder.join('');
speeding up code Cache out of scope variables Cache array.length
String-building Cache calculations
working with the dom Use CSS Keep it small (virtual
rendering) Minimise reflows/repaints position: absolute; left: 50%; margin-left: -200px; width: 400px;
Minimising reflows innerHTML/$.html over creating nodes Batch setting styles Stay
low in the DOM tree Detach/hide nodes
Minimising reflows Cache node properties Position: fixed/absolute for animations Avoid
tables for layout
HEAVY frameworks Do you really need them?
caching AJAX requests Server-side scripts Client-side caching (localStorage)
using prototypes properly Functions Value-type instance variables var Person =
function() {}; Person.prototype.doSomething = function(...) { ... }; var Person = function() { this.doSomething = function(...) { ... }; };
avoid closures Source of memory leaks Reuse static functions Adds
level to scope chain
faking it Loading screens Thumbnail previews for images Pre-loading
more resources Google/Stackoverflow jsperf.com bit.ly/canvas-perf bit.ly/high-performance-js (book) bit.ly/google-optimising-js bit.ly/repaint-reflow bit.ly/chrome-profiler
bit.ly/js-var-access
thanks! @dellsystem