Subtitle
Add speaker name here
Title slide
Title slide Subtitle Add speaker name here Optimize your Title - - PowerPoint PPT Presentation
Title slide Subtitle Add speaker name here Optimize your Title slide Javascript Subtitle Add speaker name here Sa a Nikoli Sa a Nikoli Developer at MD Systems You can find me at: drupal.org: sasanikolic @sasanikolic90
Subtitle
Add speaker name here
Title slide
Subtitle
Add speaker name here
Title slide
Saša Nikolić
Optimize your Javascript
Saša Nikolić
Developer at MD Systems You can find me at:
Share of web traffic by device
4%
Year on year change Year on year change
52%
Year on year change
43%
#
+4%
Laptops and desktops Mobile phones Tablet devices
Source: We are social
Source: Source: OpenSignal
Source: Digitaltrends Source: Source: OpenSignal
Source: Digitaltrends Source: giphy.com Source: Source: OpenSignal
Javascript has a cost
Things are getting better
Source: https://httparchive.org From 1. 2. 2016 to 1. 2. 2019 for Drupal
JavaScript Requests JavaScript bootup time JavaScript Bytes
The sum of kilobytes of all external scripts requested by the page (minified and gzipped code) The number of external scripts requested by the page The amount of CPU each script consumes per page
Time until interactive
> 15 sec!
Phases of a website’s loading experience ~Source: https://developers.google.com/web/fundamentals/performance/rail
Loading? Useful? Usable?
Table of content
Suggestions Why?
Why do we need to improve (mobile) JS performance?
How to test / audit your site
Tools to test the performance of your site based on various factors.
🚁
?
Practical tips for optimizing your JS
Common practices when writing JS code
Source: worldstrides.com
Workflow
How do we know if we have a JS problem?
MONITOR
Monitor your site
MEASURE
Lighthouse - performance audits
OPTIMIZE
Make improvements and optimizations
Tools to measure
Which tools can we use?
Tip:
> ls -l
Experiment
Do experiment one step at a time
Lighthouse
Source: developers.google.com
Lighthouse
Lighthouse
Lighthouse
Optimize the size
How can we optimize our JS?
SMALLER SIZE BETTER CACHING
To reduce time of first page load To reduce time of later page loads
Reducing the size
How can we reduce the size of our JS?
COMPRESSION
resources
PRECOMPRESSION
apache/nginx
Gains on first meaningful paint
MINIFICATION
○ Comments and white spaces are stripped ○ Shortens syntax ○ Converts code to abstract syntax tree (AST)
Reducing the size
Reduce the amount of JS code by minifying it
Reducing the size
Optimize JS source code with prepack and closure compiler
PREPACK
time instead of run time CLOSURE COMPILER
○ Parse JS ○ Analyze JS ○ Remove dead code ○ Rewrites and minimizes what's left
Reducing the size
Reduce the size with code splitting technique
CODE SPLITTING
○ Two files; for custom code and libraries
○ Chunks per route
Source: https://media.giphy.com/media/ 26h0oZIGGOFZdZj4Q/giphy.gif
Reducing the size
Remove extra dependencies with Tree Shaking
TREE SHAKING
Source: https://www.keycdn.com/ blog/tree-shaking
Reducing the size
Example of Tree Shaking technique
Reducing the size
Effective example of Tree Shaking technique
modules.js index.js
Reduced size of up to 30%
Always look for new solutions
Try new things like Next.js
NEXT.JS
○ React framework with built-in automatic code splitting, server side rendering, static exporting… ○ Switch is invisible ○ “Free” performance with few adjustments ○ Optimised code-splitting ○ Claimed 7.5x performance increase
Caching
○ Determine optimal lifetimes (max-age) ○ Supply validation tokens (ETag)
Caching
○ Rich offline experiences ○ Periodic background sync ○ Push notifications
More optimizations
More useful optimizations
○ Preload ○ Prefetch ○ Preconnect
Micro optimisations
A list of useful tips
getElementById() and constructs
○ Use the local scope
animations
Micro optimisations
A list of useful tips
○ Define variables in a global object or in closures
○ eval("obj." + id); instead obj[id]
setTimeOut
Subtitle
Add speaker name here
Title slide
Software ages like milk, not like wine
Unknown
Monitor
How can we monitor our JS?
○ Source-map explorer ○ Webpack bundle analyser
○ Do they provide any value? ○ Lazy load embeds
GOALS
CULTURE < 5s first-load
< 2s subsequent
Subtitle
Add speaker name here
Title slide
Join us for contribution opportunities
Friday, April 12, 2019
9:00-18:00 Room: 602
Mentored Contribution First Time Contributor Workshop General Contribution
#DrupalContributions
9:00-12:00 Room: 606 9:00-18:00 Room: 6A
Subtitle
Add speaker name here
Title slide
What did you think?
Locate this session at the DrupalCon Seattle website:
https://events.drupal.org/seattle2019/sessions/optimize-your-javascript
Take the Survey!
https://www.surveymonkey.com/r/DrupalConSeattle
Sources
Helpful links
execution
Subtitle
Add speaker name here
Title slide