SlideShare a Scribd company logo
Performance Optimization of
Vue.js Apps with modern JS
Filip Rakowski
@filrakowski
Filip Rakowski
Co-founder
Frontend Developer
Community partner
@filrakowski
Let’s talk about Performance!
*but not rendering performance, it’s a huge topic and they gave me only 25 minutes ;(
@filrakowski
Why performance is important?
@filrakowski
@filrakowski
2.2 sec load time reduction resulted in 15.4%
more downloads
Firefox
@filrakowski
1s slowdown resulted in 11% less page views
Aberdeen Group
@filrakowski
2 sec delay resulted in 4.3% loss in revenue
per visitor and 3.75% reduction in clicks
Bing
@filrakowski
cutting page load time half resulted in 13%
increase in sales
AutoAnything
@filrakowski
SPEED = MONEY
@filrakowski
What influences loading time performance?
@filrakowski
Request /
Response
@filrakowski
Request /
Response
Parsing
@filrakowski
Request /
Response
Parsing Execution
@filrakowski
Request /
Response
Parsing Execution
@filrakowski
Let’s make an app!
@filrakowski
main.jsApp.vue Vue
@filrakowski
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router app.js
App.vue
every new module or library
=
bigger bundle
@filrakowski
bigger bundle
=
longer download
@filrakowski
bigger bundle
=
longer parsing
@filrakowski
bigger bundle
=
longer execution
@filrakowski
SPEED = MONEY
@filrakowski
MORE FEATURES = LESS MONEY?
@filrakowski
@filrakowski
How webpack bundling works?
@filrakowski
Dependency graph is a graph of modules in
your application. Starting from entry point
webpack is recursively adding all imported
modules to the graph and bundles them into
(usually) one file.
@filrakowski
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router
App.vue
app.js
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router app.js
App.vue We might not
need this!
Code splitting allows you to split your code
into various bundles. You can think about this
as deferring a piece of your dependency
graph to be loaded later.
@filrakowski
Lazy loading loading chunk of code on demand
usually as a response to user action.
@filrakowski
Dynamic import
@filrakowski
@filrakowski
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router app.js
App.vue
home.js
about.js
Nuxt is doing per-route code-splitting out of the box!
@filrakowski
Rule #1 - Split your code per route
@filrakowski
What else we don’t need on initial download?
@filrakowski
@filrakowski
@filrakowski
Rule #2 - Load off-screen components lazily
@filrakowski
@filrakowski
You can use v-if to lazily
load off-screen components
What else?
@filrakowski
Rule #3 - Load non-critical libraries lazily
@filrakowski
@filrakowski
Rule #4 - Avoid bundling all 3rd party libs into
one file. It’s an anti-pattern.
@filrakowski
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router vendor.js
App.vue
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router vendor.js
App.vue
We might not
need this!
What about code duplication?
@filrakowski
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router app.js
App.vue
home.js
about.js
Lodash
main.jsrouter.js Vue
@filrakowski
Home.vue
About.vueLodash
vue-router app.js
App.vue
home.js
about.js
Lodash
@filrakowski
Rule #5 - Choose your libraries carefully and
try to find smaller equivalents if possible
@filrakowski
@filrakowski
@filrakowski
@filrakowski
Request /
Response
Parsing Execution
@filrakowski
Rule #5 - Make use of cache for static assets
@filrakowski
Service workers essentially act as proxy
servers that sit between web applications, the
browser, and the network (when available).
They can be used to serve network responses
from cache instead of a network.
@filrakowski
Service
Worker
Network
@filrakowski
Client app
Service
Worker
Network
@filrakowski
Client app
Cache
If asset was previously
downloaded
If asset is not in cache
or needs to be „fresh”
Vue CLI PWA plugin
@filrakowski
@filrakowski
Nuxt PWA module
@filrakowski
@filrakowski
@filrakowski
What else we can do?
@filrakowski
Prefetching downloading assets before they’re
requested.
@filrakowski
Rule #6 - Prefetch lazily loaded resources
@filrakowski
@filrakowski
More general options
webpack precachePlugin
workbox-webpack-plugin
@filrakowski
Vue-cli 3 is prefetching lazy chunks out of the box!
@filrakowski
Don’t forget about compression, minification and other
stuff that webpack and DevOps will do for you
@filrakowski
Measuring and analyzing
@filrakowski
#1 Dev tools “coverage” tool
@filrakowski
@filrakowski
CMD + Shift + P “Coverage”
@filrakowski
Everything marked with red should probably be loaded lazily
#2 Webpack bundle analyzer
@filrakowski
@filrakowski
@filrakowski
#2 ‘bundlesize’ package
@filrakowski
@filrakowski
@filrakowski
#3 VS Code ‘Import cost’ plugin
@filrakowski
@filrakowski
Thank you!
If you already forgot something - don’t worry.
I’ll share the slides :)
@filrakowski

More Related Content

What's hot (19)

PPTX
10 top web development frameworks (new version 21 11)
Mandar Majmudar
 
PPTX
Aeternity Blockchain - Ecosystem & Devtools [2019]
Przemysław Thomann
 
PPTX
Architecture & Workflow of Modern Web Apps
Rasheed Waraich
 
PPTX
What’s new in LightSwitch 2013?
Robert MacLean
 
PPTX
Micro frontends
Kleyson Prado
 
PPTX
Windows 8
Robert MacLean
 
PPTX
Visual Studio: The best tool for web developers.
Robert MacLean
 
PPTX
Micro-Frontends JSVidCon
Amir Zuker
 
PPTX
Shift Remote FRONTEND: Micro Frontend Architecture: A Look Into the Future - ...
Shift Conference
 
PPTX
Fronted development trends - past, present and the future
Harijs Deksnis
 
PPTX
Web Development and Web Development technologies - Temitayo Fadojutimi
Temitayo Fadojutimi
 
PDF
Magento 2 Module in 50 Minutes
Ben Marks
 
PDF
How To be a Backend developer
Ramy Hakam
 
PPTX
Full stack web development
Crampete
 
PPT
Joomla as a mobile App backend - ideas, examples and experiences
Andy_Gaskell
 
PDF
PWA for PHP Developers
Ben Marks
 
PDF
What Web Framework To Use?
Kasra Khosravi
 
PDF
Progressive Web Apps / GDG DevFest - Season 2016
Abdelrahman Omran
 
PPTX
Continuous workflow for a large react native app - mobile at wix
Shalom Yerushalmy
 
10 top web development frameworks (new version 21 11)
Mandar Majmudar
 
Aeternity Blockchain - Ecosystem & Devtools [2019]
Przemysław Thomann
 
Architecture & Workflow of Modern Web Apps
Rasheed Waraich
 
What’s new in LightSwitch 2013?
Robert MacLean
 
Micro frontends
Kleyson Prado
 
Windows 8
Robert MacLean
 
Visual Studio: The best tool for web developers.
Robert MacLean
 
Micro-Frontends JSVidCon
Amir Zuker
 
Shift Remote FRONTEND: Micro Frontend Architecture: A Look Into the Future - ...
Shift Conference
 
Fronted development trends - past, present and the future
Harijs Deksnis
 
Web Development and Web Development technologies - Temitayo Fadojutimi
Temitayo Fadojutimi
 
Magento 2 Module in 50 Minutes
Ben Marks
 
How To be a Backend developer
Ramy Hakam
 
Full stack web development
Crampete
 
Joomla as a mobile App backend - ideas, examples and experiences
Andy_Gaskell
 
PWA for PHP Developers
Ben Marks
 
What Web Framework To Use?
Kasra Khosravi
 
Progressive Web Apps / GDG DevFest - Season 2016
Abdelrahman Omran
 
Continuous workflow for a large react native app - mobile at wix
Shalom Yerushalmy
 

Similar to Performance optimization of vue.js apps with modern js (20)

PDF
Filip Rakowski "Web Performance in modern JavaScript world"
Fwdays
 
PDF
Web Frameworks of the Future
elliando dias
 
PDF
Long life to vagrant… Vagrant is dead
Federico Panini
 
PDF
"You’re probably using Lighthouse wrong: How do we misuse the most common too...
Fwdays
 
PDF
FFWD.PRO - It's not you, It's me (or how to avoid being coupled with a Javasc...
Marco Cedaro
 
PDF
Refactoring to Go modules: why and how
Leon Stigter
 
KEY
Phonegap 1.0
Brian LeRoux
 
PPTX
Web Components: The Future of Web Development is Here
John Riviello
 
PDF
Practical WebAssembly with Apex, wasmRS, and nanobus
Jarrod Overson
 
PPTX
Flask and Introduction to web frameworks
dipendralfs
 
PDF
Selje_Amazing VFP2C32 Library.pdf
Eric Selje
 
PPTX
Introduction to Progressive Web Apps / Meet Magento PL 2018
Filip Rakowski
 
PDF
The Big Cloud native FaaS Lebowski
QAware GmbH
 
PDF
Workshop: Introduction to Web Components & Polymer
John Riviello
 
PDF
Keypoints html5
dynamis
 
PPTX
Pain Driven Development by Alexandr Sugak
Sigma Software
 
PDF
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
🎤 Hanno Embregts 🎸
 
PDF
Ionic - Revolutionizing Hybrid Mobile Application Development
Justin James
 
PDF
Encode polkadot club
Vanessa Lošić
 
PDF
JsDay - It's not you, It's me (or how to avoid being coupled with a Javascrip...
Marco Cedaro
 
Filip Rakowski "Web Performance in modern JavaScript world"
Fwdays
 
Web Frameworks of the Future
elliando dias
 
Long life to vagrant… Vagrant is dead
Federico Panini
 
"You’re probably using Lighthouse wrong: How do we misuse the most common too...
Fwdays
 
FFWD.PRO - It's not you, It's me (or how to avoid being coupled with a Javasc...
Marco Cedaro
 
Refactoring to Go modules: why and how
Leon Stigter
 
Phonegap 1.0
Brian LeRoux
 
Web Components: The Future of Web Development is Here
John Riviello
 
Practical WebAssembly with Apex, wasmRS, and nanobus
Jarrod Overson
 
Flask and Introduction to web frameworks
dipendralfs
 
Selje_Amazing VFP2C32 Library.pdf
Eric Selje
 
Introduction to Progressive Web Apps / Meet Magento PL 2018
Filip Rakowski
 
The Big Cloud native FaaS Lebowski
QAware GmbH
 
Workshop: Introduction to Web Components & Polymer
John Riviello
 
Keypoints html5
dynamis
 
Pain Driven Development by Alexandr Sugak
Sigma Software
 
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
🎤 Hanno Embregts 🎸
 
Ionic - Revolutionizing Hybrid Mobile Application Development
Justin James
 
Encode polkadot club
Vanessa Lošić
 
JsDay - It's not you, It's me (or how to avoid being coupled with a Javascrip...
Marco Cedaro
 
Ad

Recently uploaded (20)

PDF
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
internet básico presentacion es una red global
70965857
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PDF
Paper: Quantum Financial System - DeFi patent wars
Steven McGee
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
internet básico presentacion es una red global
70965857
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
Orchestrating things in Angular application
Peter Abraham
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
Paper: Quantum Financial System - DeFi patent wars
Steven McGee
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Ad

Performance optimization of vue.js apps with modern js

Editor's Notes

  • #4: How many of you think performance is important?
  • #5: For those who did not raised their hands I will pretend that I’m smart and show some numbers to prove my point that web performance is extremely important.
  • #6: First of all as you can see only one second of waiting is enough for the user to make a mental context switch and potentially leave our website. Now let’s see how performance affects other numbers
  • #11: Given all this number we can assume a simple equation. Speed equals money. Do you agree?
  • #12: Now when I hope you’re convinced you how important web performance is let’s find main factors affecting it
  • #13: When we want some resource we need to request it and wait for resource. The bigger file is - the longer it takes to download it.
  • #14: Next JavaScript engine needs to parse it. Again the bigger file is - the longer it takes.
  • #15: Once parsing is finished and our JS engine created a nice AST that browser can work with it’s time to execute the code and guess what - the more code it’s to execute the longer it takes.
  • #16: Ok so it looks like the main factor influencing loading performance is file size. As a frontend developers we mostly can optimize the execution and parsing phase but we will see later that there are some things that we can do to improve response time. For now let’s focus on the area that we certainly can fully control.
  • #17: Let’s see what problems we can encounter by creating simple app
  • #18: It usually starts like this. An entry point with Vue attached and a root component. But over time the application grows
  • #19: We almost certainly will add a Vue-router. Following this we will add some routes, probably another 3rd party libs.
  • #20: Our initially downloaded JS bundle will grow with every module we add
  • #24: As we remember speed equals money so does it mean that more features equals less money?
  • #26: As you probably guessed it doesn’t To understand how to solve performance issues with growing bundle we need to understand how this bundle is constructed
  • #28: It appears that web pack under the hood is creating something called dependency graph. It’s just recursively checks the imports in JS modules and adds them to the graph starting from your entry point. To better understand this process lets see an example
  • #35: The problem with bundling all modules into one file is that we might not need some of the modules depending on current state of the application
  • #36: This is why webpack has feature called code splitting. It allows you to split your code into different bundles and load them on demand.
  • #37: Lazy loading is just loading this code splitter chunks on demand. So we are cutting some modules and loading them when they are needed
  • #38: We can tell webpack which parts of the code it should split by doing a dynamic import. Instead of a regular import dynamically imported module is an entry point for a completely new bundle. The bundle will be lazily loaded ONLY when this function is invoked.
  • #39: By dynamically importing routes we are making sure that only the visited route will be downloaded
  • #40: Code-splitter JS bundles generated by webpack will look like this.We have three bundles instead of one big file
  • #41: If you are using Nuxt you have this feature out of the box
  • #42: So rule number one… Per-route code splitting will give you more than any other lazy code splitting technique but we can do more
  • #43: There are still many things that are not needed right after user enters your website and can be loaded lazily
  • #44: Like popups
  • #45: Sidebars or any other off-screen components
  • #46: So rule number 2
  • #47: You can use v-if to conditionally load offscreen compoennts. V-if not true -> no render - no invocation - no download. It’s a great way to conditionally load components
  • #49: It appears that most of the 3rd party libs can also be loaded lazily.
  • #50: And the syntax is the same. Import function returns a promise with the lazily loaded lib
  • #51: When speaking about 3rd party libs there is a common approach to keep all of node modules in vendor bundle.
  • #53: Don’t do this. It’s much better to k keep 3rd party libs in route bundles or even better load them lazily whenever it’s possible. Otherwise users will download redundant code
  • #54: You’ll probably say ‚Hey Filip it leads to code duplication aren;’t vendor bundles meant to prevent it?’
  • #55: What if we will have loads imported in two routes. How to deal with this duplication?
  • #57: Now the shared modules will be bundled into a separate file and therefore downloaded only once
  • #58: Staying in the 3rd party libs topic there are tons of them and not all of them are good for your use case, or good overall so choose them carefully
  • #59: There is a great website that can help you with choosing appropriate libraries - bundle phobia
  • #60: It will give you all the information on how adding some particular library will affect your apps performance
  • #61: what is even better - it will also suggest you alternatives so you can really choose the best option
  • #62: Previously I mentioned that as a frontend devs we don’t have influence on how fast the server responds but it turns out that there are some things that we can do to improve this part of the process
  • #63: We can reduce the number of network requests by making use of assets that we already downloaded
  • #64: And we can use SW for this. They work as a proxy between your client web app and server
  • #65: We are sending request to the service worker which proxies it to the network
  • #66: But it can also proxy this request to the cache if asset was previously downloaded and respond almost instantly. Service Worker cache persists even when browser has been closed.
  • #67: You can either install Vue-cli PWA plugin for this
  • #70: ..or Nuxt PWA module
  • #71: In both cases you will see cached data under application tab of your devtools. Of course you can configure it as you wish
  • #72: We can make use of prefetching
  • #73: Which is just downloading assets before they are needed
  • #74: So rule number 5
  • #75: We can explicitly tell which chunks of code we want to prefetch with web pack magic comments that will dynamically add prefetch link to the head of our application
  • #76: Or use more general solutions with regular expressions
  • #77: If you are using Vue-cli 3 it’s prefetching code-splitter chunks out of the box
  • #78: If you are using Vue-cli 3 it’s prefetching code-splitter chunks out of the box
  • #79: Ok we learned a lot about performance optimization but it’s equally important to know where to optimize and measure our results.
  • #80: You can use devtools „coevrage” tool to see how much of the shipped code was actually executed
  • #83: You can also use Webpack Bundle Analyzer to actually see the size of your modules. This one is a must-have.
  • #85: …and instalation is extremely easy
  • #86: You can use ‚bundlesize’ package to make sure that you are not exceeding the reasonable size of the package.
  • #88: ..it also has a very nice GitHub integration
  • #89: And you can use import cost plugin to keep track of size of your imports