SlideShare a Scribd company logo
Nir Kaufman
ANGULAR
PERFORMANCE
crash course
Nir Kaufman
@nirkaufman
Google Developer Expert
Worldwide speaker
Community leader
Tel Aviv • New York
Boosting Angular runtime performance
LEARN
THE BASICS
RENDERING TIME
Change Detection Browser Rendering
Events Handlers Data bindings
Total Rendering Time
Trigger DOM updates
2 PHASES OF CHANGE DETECTION
Property checks and DOM updates
are performed on Views.
ANGULAR VIEWS
linker/view_ref.ts
export interface ViewData {
def: ViewDefinition;
root: RootData;
renderer: Renderer2;
parentNodeDef: NodeDef|null;
parent: ViewData|null;
viewContainerParent: ViewData|null;
component: any;
context: any;
nodes: {[key: number]: NodeData};
state: ViewState;
oldValues: any[];
disposables: DisposableFn[]|null;
}
export interface ViewData {
def: ViewDefinition;
root: RootData;
renderer: Renderer2;
parentNodeDef: NodeDef|null;
parent: ViewData|null;
viewContainerParent: ViewData|null;
component: any;
context: any;
nodes: {[key: number]: NodeData};
state: ViewState;
oldValues: any[];
disposables: DisposableFn[]|null;
}
class ViewRef extends ChangeDetectorRef
REFERENCE TO THE VIEW
constructor(cdr:ChangeDetectorRef) {
// refer to this component view
}
abstract class ChangeDetectorRef {
abstract checkNoChanges(): void;
abstract detach() : void;
abstract detectChanges() : void;
abstract markForCheck() : void;
abstract reattach() : void;
}
CHANGE DETECTOR
abstract class ChangeDetectorRef {
abstract checkNoChanges(): void;
abstract detach() : void;
abstract detectChanges() : void;
abstract markForCheck() : void;
abstract reattach() : void;
}
CHANGE DETECTOR
Run change detection once 
for the current component
and all its children.
DETECT CHANGES
Run change detection once 
for the current component
and all its children.
DETECT CHANGES
tick(): void {
...
try {
this._runningTick = true;
this._views.forEach((view) =>
view.detectChanges());
…
}
appilcation_ref.ts
DETECT CHANGES
this._zone.onMicrotaskEmpty.subscribe({
next: () => {
this._zone.run(() => {
this.tick();
});
}
});
DETECT CHANGES
this._zone.onMicrotaskEmpty.subscribe({
next: () => {
this._zone.run(() => {
this.tick();
});
}
});
DETECT CHANGES
Trigger change detection
as a result of async
operations.
NG ZONE
NG ZONE
export class NgZone {
private outer: Zone;
private inner: Zone;
constructor({enableLongStackTrace = false}) {
if (typeof Zone == 'undefined') {
throw new Error('Angular requires Zone.js prolyfill.');
}
}
}
NG ZONE
export class NgZone {
private outer: Zone;
private inner: Zone;
constructor({enableLongStackTrace = false}) {
if (typeof Zone == 'undefined') {
throw new Error('Angular requires Zone.js prolyfill.');
}
}
}
Boosting Angular runtime performance
NG ZONE
export class NgZone {
private outer: Zone;
private inner: Zone;
constructor({enableLongStackTrace = false}) {
if (typeof Zone == 'undefined') {
throw new Error('Angular requires Zone.js prolyfill.');
}
}
}
ng_zone.ts
RENDERING TIME
Change Detection Browser Rendering
Events Handlers Data bindings
Total Rendering Time
Trigger DOM updates
OUR GOAL
Reducing CD executions.
Reducing the execution time.
Controlling the CD flow.
OPTIMIZE
YOUR CALLBACKS
All events handlers
must complete
Before change detection
can continue.
OPTIMIZING CALLBACKS
All events handlers
must complete
Before change detection
can continue.
OPTIMIZING CALLBACKS
Return fast from callbacks.
Use of input setters instead onChanges.
Prevent unnecessary callbacks.
Prefer pure pipes.
Cache wherever you can.
THE CALLBACK CHECKLIST
RETURN FAST.
USE INPUT SETTERS
@Input() set value (value: any) {
// executed on this input change
}
ngOnChanges(changes: SimpleChanges){
// executed on any input change
}
PREVENT UNNECESSARY CALLBACKS
this.control
.valueChanges
.debounceTime(350)
.subscribe(...);
PREVENT UNNECESSARY CALLBACKS
[ngModelOptions]="{updateOn:'blur'}
PREVENT UNNECESSARY CALLBACKS
.subscribe( newValue => {
if( newValue !== this.oldValue ) {
this.customEvent.emit(newValue)
}
PREFER PURE PIPES
[users]="showActiveUsers()"
[users]="users | showActive"
Each CD
Only if users changes
CACHE CALLBACK RESULTS
@memoize()
calculateCode(nat, gender) {
// return user code
}
OPTIMIZE
CHANGE DETECTION
THE CHECKLIST
Use onPush when possible
Exclude components from CD
Implement your own CD logic
Boosting Angular runtime performance
class ChangeDetectorRef {
checkNoChanges()
detach()
detectChanges()
markForCheck()
reattach()
}
CHANGE DETECTION
@Input() items;
constructor(
private cdr: ChangeDetectorRef){}
ngOnInit(){
this.cdr.detach();
}
ngOnChanges(changes: SimpleChanges) {
this.cdr.detectChanges();
}
TAKE CONTROL
DISCOVER LIFE
OUTSIDE THE ZONE
OUTSIDE THE ZONE
constructor(private zone: NgZone ) {
zone.runOutsideAngular()
}
GOING MANUAL
platformBrowserDynamic()
.bootstrapModule(AppModule, {
ngZone: 'noop'
})
NEXT STEPS
CHOOSE YOUR PATH
github.com/demo-projects/ng_poland_performance
DON’T BE AFRAID OF THE SOURCE
READ THIS BLOG
https://blog.angularindepth.com/
KEEP IN TOUCH
THANK YOU!
@nirkaufman

More Related Content

PDF
How Angular2 Can Improve Your AngularJS Apps Today!
PDF
Redux with angular 2 - workshop 2016
PDF
Evan Schultz - Angular Camp - ng2-redux
PDF
Redux pattens - JSHeroes 2018
PDF
An introduction to Angular2
PDF
Evan Schultz - Angular Summit - 2016
PDF
Angular2 & ngrx/store: Game of States
PDF
Reactive.architecture.with.Angular
How Angular2 Can Improve Your AngularJS Apps Today!
Redux with angular 2 - workshop 2016
Evan Schultz - Angular Camp - ng2-redux
Redux pattens - JSHeroes 2018
An introduction to Angular2
Evan Schultz - Angular Summit - 2016
Angular2 & ngrx/store: Game of States
Reactive.architecture.with.Angular

What's hot (20)

PDF
Angular2 workshop
PDF
redux and angular - up and running
PDF
Workshop 23: ReactJS, React & Redux testing
PDF
Modern Web Developement
PDF
Angular promises and http
PDF
Gerenciamento de estado no Angular com NgRx
PPTX
Angular2 + rxjs
PDF
Workshop 14: AngularJS Parte III
PDF
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
PDF
Angular server-side communication
PDF
Angular js 2.0, ng poznań 20.11
PDF
Angular 2.0 - What to expect
PDF
Reactive, component 그리고 angular2
PDF
Workshop 20: ReactJS Part II Flux Pattern & Redux
PDF
Introduction to ReactJS and Redux
PDF
Intro to Redux | DreamLab Academy #3
PDF
Creating a WYSIWYG Editor with React
PDF
Advanced redux
PDF
React + Redux. Best practices
Angular2 workshop
redux and angular - up and running
Workshop 23: ReactJS, React & Redux testing
Modern Web Developement
Angular promises and http
Gerenciamento de estado no Angular com NgRx
Angular2 + rxjs
Workshop 14: AngularJS Parte III
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Angular server-side communication
Angular js 2.0, ng poznań 20.11
Angular 2.0 - What to expect
Reactive, component 그리고 angular2
Workshop 20: ReactJS Part II Flux Pattern & Redux
Introduction to ReactJS and Redux
Intro to Redux | DreamLab Academy #3
Creating a WYSIWYG Editor with React
Advanced redux
React + Redux. Best practices
Ad

Similar to Boosting Angular runtime performance (20)

PDF
ez-clang C++ REPL for bare-metal embedded devices
ODP
Nagios Conference 2012 - Dave Josephsen - Stop Being Lazy
PDF
Performance measurement and tuning
PDF
How to build rock solid apps & keep 100m+ users happy
PPTX
Ob1k presentation at Java.IL
PDF
Native Java with GraalVM
PDF
ProxySQL at Scale on AWS.pdf
PPSX
Dx11 performancereloaded
ODP
Trash Robotic Router Platform - David Melendez - Codemotion Rome 2015
PDF
Docker swarm introduction
PDF
How to build rock solid apps & keep 100m+ users happy
PDF
Delivering Go.CD with Terraform and Docker
PPTX
Moving from Jenkins 1 to 2 declarative pipeline adventures
PDF
The powerful toolset of the go-mysql library
PDF
Easy Cloud Native Transformation using HashiCorp Nomad
PDF
dSS API by example
PDF
Streaming SQL for Data Engineers: The Next Big Thing?
PDF
Streaming SQL for Data Engineers: The Next Big Thing? With Yaroslav Tkachenko...
PDF
Implementing New Web
PDF
Implementing new WebAPIs
ez-clang C++ REPL for bare-metal embedded devices
Nagios Conference 2012 - Dave Josephsen - Stop Being Lazy
Performance measurement and tuning
How to build rock solid apps & keep 100m+ users happy
Ob1k presentation at Java.IL
Native Java with GraalVM
ProxySQL at Scale on AWS.pdf
Dx11 performancereloaded
Trash Robotic Router Platform - David Melendez - Codemotion Rome 2015
Docker swarm introduction
How to build rock solid apps & keep 100m+ users happy
Delivering Go.CD with Terraform and Docker
Moving from Jenkins 1 to 2 declarative pipeline adventures
The powerful toolset of the go-mysql library
Easy Cloud Native Transformation using HashiCorp Nomad
dSS API by example
Streaming SQL for Data Engineers: The Next Big Thing?
Streaming SQL for Data Engineers: The Next Big Thing? With Yaroslav Tkachenko...
Implementing New Web
Implementing new WebAPIs
Ad

More from Nir Kaufman (20)

PDF
Angular Dependency Injection
PDF
Angular Prestige: Less-known API and techniques
PDF
Angular CLI custom builders
PDF
Electronic music 101 for developers
PDF
Nestjs MasterClass Slides
PDF
Angular EE - Special Workshop by Nir Kaufman
PDF
Decorators in js
PDF
Styling recipes for Angular components
PDF
Introduction To Angular's reactive forms
PDF
Webstorm
PDF
Angular Pipes Workshop
PDF
Data Structures in javaScript 2015
PDF
Angular redux
PDF
Solid angular
PDF
Webpack and angularjs
PDF
Angular2 - getting-ready
PDF
Up & running with ECMAScript6
PDF
AngularJS performance & production tips
PDF
AngularJS - Services
PDF
Angular js - 10 reasons to choose angularjs
Angular Dependency Injection
Angular Prestige: Less-known API and techniques
Angular CLI custom builders
Electronic music 101 for developers
Nestjs MasterClass Slides
Angular EE - Special Workshop by Nir Kaufman
Decorators in js
Styling recipes for Angular components
Introduction To Angular's reactive forms
Webstorm
Angular Pipes Workshop
Data Structures in javaScript 2015
Angular redux
Solid angular
Webpack and angularjs
Angular2 - getting-ready
Up & running with ECMAScript6
AngularJS performance & production tips
AngularJS - Services
Angular js - 10 reasons to choose angularjs

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Electronic commerce courselecture one. Pdf
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Chapter 2 Digital Image Fundamentals.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Sensors and Actuators in IoT Systems using pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Electronic commerce courselecture one. Pdf
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Spectral efficient network and resource selection model in 5G networks
madgavkar20181017ppt McKinsey Presentation.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Chapter 2 Digital Image Fundamentals.pdf

Boosting Angular runtime performance