SlideShare a Scribd company logo
How To Tweak Angular 2 Performance (JavaScript Frameworks Day 2017 Kiev)
12822 April 2017 2
What is 128 hours
• 3 working weeks and 1 day.
• 4 / 5 of your yearly vacation plan.
• Some money.
• Put your option here.
22 April 2017 3
Agenda
• Why does performance matter?
• Why do we need to tweak Angular 2?
• Problems we need to solve
• Solutions
• Conclusions
22 April 2017 4
What can we improve?
• Bundle size
• Application performance
22 April 2017 5
Infrastructure Improvements
22 April 2017 6
Bundle size: problem
22 April 2017 7
Not optimized FE 2752kb
Images Fonts HTML Scripts CSS
Bundle size: problem
22 April 2017 8
Optimized FE 822 kb
Images Fonts HTML Scripts CSS
Bundle size: problem
22 April 2017 9
0 50 100 150 200 250 300 350 400
2.5 Mb
1.5 Mb
0.5Mb
Loadingtime
56,6 kbit/s 1 Mbit/s 5 Mbit/s 50 Mbit/s
Ok. Why does it matter?
80% of internet users own a smartphone. (Smart Insights)
22 April 2017 10
…
Over 50% of smartphone users grab their smartphone
immediately after waking up. (ExpressPigeon, 2014)
22 April 2017 11
…
Google says 61% of users are unlikely to return to a mobile
site they had trouble accessing and 40% visit a competitor’s
site instead. (MicKinsey & Company)
22 April 2017 12
Ok. Why does it matter?
How can we reduce the bundle size?
• Compress pictures
• Minify styles, templates and scripts
• Remove useless code
22 April 2017 14
Handling images
• gulp-image-optimization
• gulp-image
• gulp-imagemin
• image-webpack-loader
…
22 April 2017 15
Handling styles
• A variety of Webpack loaders
• Angular CLI supports it by design
22 April 2017 16
Handling scripts
• A variety of Webpack loaders
• Angular CLI supports it by design
• … it is still huge. So we need to remove useless code.
22 April 2017 17
Tree Shaking
22 April 2017 18
0 500 1000 1500 2000 2500
Application 1
Application 2
Optimization Results
Before Tree Shaking,Kb With Tree Shaking,Kb
22 April 2017
Tree Shaking
- Webpack marks useless code
- UglifyJsPlugin removes the code
How does tree shaking work?
22 April 2017 20
How does tree shaking work?
22 April 2017 21
How does tree shaking work?
22 April 2017 22
Tree Shaking
• For the moment Angular CLI has problems with tree
shaking.
• Use the latest version of Angular CLI.
• You need TypeScript 2 and WebPack 2.
22 April 2017 23
Compilation
22 April 2017 24
Compilation
• Just In Time Compilation (JIT)
• Ahead Of Time Compilation (AOT)
22 April 2017 25
What is JIT?
• Compiles in the browser.
• No need to build after changes.
• Default compiler in Angular 2 CLI.
22 April 2017 26
Why is AOT better?
• You don’t need to load compiler anymore.
• Faster loading.
• Better runtime performance.
• AOT is more secure, because JIT uses eval.
22 April 2017 27
AST (Abstract Syntax Tree)
22 April 2017 28
Script
Container
Members Decorators
Container
Members Decorators
Container
Members Decorators
AOT (Ahead Of Time Compilation)
22 April 2017 29
0 0.5 1 1.5 2 2.5 3
Bundle size, mb
AngularCLI defaultprojectsize
Compression,tree shaking and AOT
Compression with Tree Shaking
Raw
What can we improve?
• Bundle size
• Application performance
22 April 2017 30
Architecture Improvements
22 April 2017 31
Does architecture matter on
Frontend?
22 April 2017 32
What we can achieve with good
architecture?
• Better application performance
• Fewer API requests (app drives faster)
• Faster loading speed
22 April 2017 33
What architecture features are we
talking about?
• Make sure we’re using events efficiently.
• Make sure our data layer uses data efficiently.
• Control assets loading process.
• Organize correct components composition.
• Reduce DOM overhead.
22 April 2017 34
What to do with events?
• Use correct lifecycle hooks in the app.
• Be careful with your own events and use destructors.
• Avoid duplication of events.
• Avoid creation of useless events.
22 April 2017 35
What can we do with a data layer?
• Store static data in the browser. (Caching)
• Use pure RESTAPI (With HATEOAS)
• Use debouncing. Really.
• Background services.
22 April 2017 36
HATEAOS
22 April 2017 37
22 April 2017
ReactiveX (Rx.js)
• One more way to organize the
asynchronous actions using JavaScript
• Leads to a new way of the components’
composition.
Problem: XHR Overhead
22 April 2017 39
Rx.js + A2
PROS
- Allows us to remove HTTP logic from the components.
- Very easy to use with the ‘async’ pipe.
CONS
- Leads to a new way of the components’ composition.
- It isn’t a fastest solution.
22 April 2017 40
What can we do with component’s
composition?
22 April 2017 41
Components Composition
22 April 2017 42
Problem: Memory Leaks
22 April 2017 43
Solution: Zone.js, ngZones, Data
Composition
• Dying zone will remove the subscriptions for local
streams, but won’t do it to current ones in service, upper-
level components.
• Use ngOnDestroy lifecycle hook for removing such
subscriptions.
22 April 2017 44
Zone.js, ngZones, Data Composition
22 April 2017 H T T P S : / / G I T H U B . C O M / A N G U L A R / Z O N E . J S / 45
Problem: Binding Overhead
• A simple rule: don’t use two-way binding when you don’t
need it.
• ‘Singular’ binding isn’t shipped out of the box, but there
are several ways to implement it.
22 April 2017 46
22 April 2017
Binding
One-directional «inside»:
• {{expression}}
• [target] = "expression"
• bind-target = "expression"
One-directional «inside»:
22 April 2017 48
22 April 2017
Binding
One-directional «outside»:
• (target) = "statement"
• on-target = "statement"
One-directional «outside»:
22 April 2017 50
22 April 2017
Binding
Two-directional:
• [(target)] = "expression"
• bindon-target = "expression"
Two-directional:
22 April 2017 52
Problem: army of listeners
22 April 2017 53
Solution: Change Detection
Strategies
• OnPush
• Default
22 April 2017 54
Change Detection Strategies
22 April 2017 55
What to with the DOM?
• Remove and add the elements back instead of hiding /
displaying.
• Don’t call DOM directly if it’s possible.
• When it isn’t possible use ElementRef,
@ViewChild/@ViewChildren.
22 April 2017 56
THANKS!
22 April 2017 57

More Related Content

PDF
Front end architecture patterns
PPTX
Angular 2 On Production (IT Talk in Dnipro)
PDF
How To Tweak Angular 2 Performance
PPTX
Александр Трищенко "How to improve Angular 2 performance?"
PDF
Grammarly Meetup: DevOps at Grammarly: Scaling 100x
PDF
Wevquery: Testing Hypotheses about Web Interaction Patterns
PDF
GraphConnect 2014 SF: How eBay and Shutl Deliver Even Faster Using Neo4j
PDF
Cypher for Apache Spark
Front end architecture patterns
Angular 2 On Production (IT Talk in Dnipro)
How To Tweak Angular 2 Performance
Александр Трищенко "How to improve Angular 2 performance?"
Grammarly Meetup: DevOps at Grammarly: Scaling 100x
Wevquery: Testing Hypotheses about Web Interaction Patterns
GraphConnect 2014 SF: How eBay and Shutl Deliver Even Faster Using Neo4j
Cypher for Apache Spark

What's hot (20)

PPTX
#ITsubbotnik Spring 2017: Anton Shapin, Denis Klykov "Visualization, storage ...
PPTX
Whats New in Serverless - Serverless Meetup AKL
PPTX
.NET Fest 2017. Константин Проскурдин. Marten как хранилище документов для .N...
PPTX
Lap around ASP.NET 5 - Dayton UG
PPTX
Free Your On-Premises Data
PDF
How to GraphQL: React Apollo
PPTX
Modern .NET Apps - Codestock
PDF
kintoneがAWSで目指すDevOpsQAな開発
PDF
Cylc - the Python workflow engine for cycling systems.
PPTX
Data analytics at a petabyte scale final
PDF
Shipments app door Tim Mercken
PDF
Distributed deep learning
PDF
GraphQL and Live Queries by Rodrigo Muñoz
PPTX
Microsoft Graph – Subscription API
PDF
2018-10-23 7 C - Using Graph API to read outlook mail for accounting - Hansam...
PDF
Graphql
PDF
Distributed Deep Learning (And How to Get Involved)
PPTX
Quix presto ide, presto summit IL
PDF
De git à la blockchain
PDF
React Fiber
#ITsubbotnik Spring 2017: Anton Shapin, Denis Klykov "Visualization, storage ...
Whats New in Serverless - Serverless Meetup AKL
.NET Fest 2017. Константин Проскурдин. Marten как хранилище документов для .N...
Lap around ASP.NET 5 - Dayton UG
Free Your On-Premises Data
How to GraphQL: React Apollo
Modern .NET Apps - Codestock
kintoneがAWSで目指すDevOpsQAな開発
Cylc - the Python workflow engine for cycling systems.
Data analytics at a petabyte scale final
Shipments app door Tim Mercken
Distributed deep learning
GraphQL and Live Queries by Rodrigo Muñoz
Microsoft Graph – Subscription API
2018-10-23 7 C - Using Graph API to read outlook mail for accounting - Hansam...
Graphql
Distributed Deep Learning (And How to Get Involved)
Quix presto ide, presto summit IL
De git à la blockchain
React Fiber
Ad

Similar to How To Tweak Angular 2 Performance (JavaScript Frameworks Day 2017 Kiev) (20)

PPTX
Making Angular2 lean and Fast
PDF
Angular Optimization Web Performance Meetup
PDF
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
PDF
Angular (v2 and up) - Morning to understand - Linagora
PDF
Angular - Improve Runtime performance 2019
PDF
Angular performance slides
PDF
Angular v2 et plus : le futur du développement d'applications en entreprise
PPTX
Advanced angular
PDF
Angular meetup 2 2019-08-29
PDF
Rethinking Angular Architecture & Performance
PDF
Myths of Angular 2: What Angular Really Is
PPTX
Intro to Angular @GDG Wolverhampton DEV-FEST 2024
PDF
Angular js mobile jsday 2014 - Verona 14 may
PDF
Angular Extreme Performance - V2
PDF
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]
PDF
Vered Flis: Because performance matters! Architecture Next 20
PPTX
NTBT #1 "Client-Side JavaScript"
PPTX
Angular Best Practices To Build Clean and Performant Web Applications
PDF
The 4W's of Angular
PPTX
Performance optimization in JS. Based on project experience, Аліна Синявська ...
Making Angular2 lean and Fast
Angular Optimization Web Performance Meetup
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
Angular (v2 and up) - Morning to understand - Linagora
Angular - Improve Runtime performance 2019
Angular performance slides
Angular v2 et plus : le futur du développement d'applications en entreprise
Advanced angular
Angular meetup 2 2019-08-29
Rethinking Angular Architecture & Performance
Myths of Angular 2: What Angular Really Is
Intro to Angular @GDG Wolverhampton DEV-FEST 2024
Angular js mobile jsday 2014 - Verona 14 may
Angular Extreme Performance - V2
26 top angular 8 interview questions to know in 2020 [www.full stack.cafe]
Vered Flis: Because performance matters! Architecture Next 20
NTBT #1 "Client-Side JavaScript"
Angular Best Practices To Build Clean and Performant Web Applications
The 4W's of Angular
Performance optimization in JS. Based on project experience, Аліна Синявська ...
Ad

More from Oleksandr Tryshchenko (8)

PDF
PWA to React Native migration
PDF
Web Scraping
PDF
PPTX
Mobile Applications with Angular 4 and Ionic 3
PDF
20 000 Leagues Under The Angular 4
PDF
ES6 Generators On Koa.js Example
PDF
Angular 2 On Production
PDF
PWA to React Native migration
Web Scraping
Mobile Applications with Angular 4 and Ionic 3
20 000 Leagues Under The Angular 4
ES6 Generators On Koa.js Example
Angular 2 On Production

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation

How To Tweak Angular 2 Performance (JavaScript Frameworks Day 2017 Kiev)