KEMBAR78
Make JavaScript Faster | PPTX
Make
JavaScript
Faster
Steve Souders
@souders
We should forget about
small efficiencies, say
about 97% of the time:
premature optimization
is the root of all evil.
Yet we should not pass
up our opportunities in
that critical 3%.
- Don Knuth
“
Start Render
First Paint
First Contentful Paint
First Meaningful Paint
DOM Interactive
DOM Content Loaded
DOM Complete
First Painted Hero
Last Painted Hero
Speed Index
First CPU Idle
Time to Interactive
Page Load
Visually Complete
0s 1s 2s 3s 4s 5s 6s
TTFB
DOM Content Loaded
Page Load
First Meaningful Paint
Speed Index
DOM Interactive
DOM Complete
First Contentful Paint
First CPU Idle
Time to Interactive
Visually Complete
medians from 1.3M sites
source: httparchive.org
First Paint
0s 2s 4s 6s 8s 12s 16s
TTFB
DOM Content Loaded
First Meaningful Paint
Speed Index
DOM Interactive
DOM Complete
First Contentful Paint
Time to Interactive
Visually Complete
mobile medians from 1.3M
sites
source: httparchive.org
1s 3s 5s 7s 9s 10s 15s14s13s11s
First Paint
First CPU Idle
Page Load
IE 7
source: Pat Meenan
Preloader
(aka "Lookahead Parser")
Biggest performance improvement EVER!
Scans HTML for tags that make requests:
SCRIPT, LINK, IMG, etc.
Prioritizes and initiates requests before
tags are parsed.
source: speedcurve.com
IE 7
source: Pat Meenan
source: speedcurve.com
browserscope.org
preloader in IE 8
IE 8
Opera 12
Safari 4
Firefox 3.5
Chrome 2
preloader
2009 2010 2011 2012 2013 2014
source: browserscope.org
source: speedcurve.com
IE 8
Opera 12
Safari 4
Firefox 3.5
Chrome 2
IE 10
Chrome 10
Firefox 3.6
Opera 14
Safari 6
preloader
async
2009 2010 2011 2012 2013 2014
source: browserscope.org
async hacks
source: speedcurve.com
async adoption
source: browserscope.org
Make JavaScript Faster
• load scripts async
• even better, prefer defer
source: speedcurve.com
Get scripts downloading earlier:
<link rel="preload"
href="main.js"
as="script">
Response header is even better:
Link: <main.js>;
rel="preload";
as="script"
https://www.chromestatus.com/metrics/feature/timeline/popularity/901
source: httparchive.org
Sites Using Preload
...for scripts in the main page
Make JavaScript Faster
• load scripts async
• even better, prefer defer
• preload scripts
source: speedcurve.com
median CPU time from 1.3M sites
JS = 433ms
source: httparchive.org
JS = 3648ms
source: httparchive.org
95th percentile CPU time from 1.3M sites
JS = 8113ms
source: httparchive.org
99th percentile CPU time from 1.3M sites
Mobile median CPU time from 1.3M sites
JS = 1121ms
source: httparchive.org
JS = 9833ms
source: httparchive.org
Mobile 95th percentile CPU time from 1.3M sites
JS = 19161ms
source: httparchive.org
Mobile 99th percentile CPU time from 1.3M sites
mobile 99th
mobile 95th
mobile median
desktop 95th
desktop median
desktop 99th
CPU time
Make JavaScript Faster
• load scripts async
• even better, prefer defer
• preload scripts
• reduce CPU time: EvaluateScript & FunctionCall
source: httparchive.org
4
12
6
5
JS requests
source: httparchive.org
53 K
258 K
106 K32 K
JS size
source: httparchive.org
4
12
6
5
53 K
258 K
106 K32 K
Make JavaScript Faster
• load scripts async
• even better, prefer defer
• preload scripts
• reduce CPU time: EvaluateScript & FunctionCall
• budget 3rd parties
Gzip missed opportunities
1. http://parts.blog.livedoor.jp/js/jquery-1.7.1.min.js
2. http://clap.blogcms.jp/js/jquery-1.11.1.min.js
3. https://cdn.schoolloop.com/.../jquery-1.8.2.min.js
4. https://c.sd1.fr/js/jquery-1.8.3.min.js
5. https://d1bxh8uas1mnw7.cloudfront.net/.../altmetric_badges...js
6. https://frontpagegui.linkfire.com/.../jquery.min.js
7. https://www.blogmura.com/.../jquery-1.11.1.min.js
8. http://static.tumblr.com/.../jquery.js
9. https://tags.onscroll.com/.../tag.min.js
10. http://d1ldansk2in7nt.cloudfront.net/.../v-1.7.1/jquery.min.js
Most Popular 1st Party
Ungzipped (90-100KB)
Make JavaScript Faster
• load scripts async
• even better, prefer defer
• preload scripts
• reduce CPU time: EvaluateScript & FunctionCall
• budget 3rd parties
• double-check compression
Make JavaScript Faster
• load scripts async
• even better, prefer defer
• preload scripts
• reduce CPU time: EvaluateScript & FunctionCall
• budget 3rd parties
• double-check compression
• review code coverage
@souders
stevesouders.com/talks

Make JavaScript Faster