SlideShare a Scribd company logo
02.07.2016
1
Databinding and
Performance-Tuning
in Angular 2
Manfred Steyer
Contents
• How databinding works
• How to improve performance with Immutables and Observables
02.07.2016
2
About me …
• Manfred Steyer
• SOFTWAREarchitekt.at
• Trainer & Consultant
• Focus: Angular
Page  3
Manfred Steyer
Bindings in Angular 2
Page  4
02.07.2016
3
Some Goals of Angular 2
Page  5
Performance Components
Predictability
Data-Binding in Angular 1.x
Page  6
Model Model Directive
Nearly everything can depend on everything
Solution: Multiple Digest-Cycles
02.07.2016
4
Component-Tree in Angular 2
Page  7
Component for whole App
Component (e. g. list)
Component
(e. g. list-item)
Component
(e. g. list-item)
Rules for Property-Bindings
• A Component only depends on its own data (and indirectly on its
parents data)
• A Component must not depend on its children data!
• Dependency Graph is a tree
• Angular only needs one iteration („digest“)
to update tree
Page  9
02.07.2016
5
Property-Binding
Page  10
model
item item
{{ item.title }} {{ item.title }}
[http://victorsavkin.com/post/110170125256/change-detection-in-angular-2]
Event-Bindings (One-Way, Bottom/Up)
Page  14
{{ item.title }} {{ item.title }}
Event-Handler
Event-Handler
02.07.2016
6
Event-Bindings (One-Way, Bottom/Up)
• No Digest-Iterations for propagating events
• But: Events can change state 
Digest for updating Property-Bindings
Page  15
Property- and Event-Bindings
Page  17
Performing
Property-Binding
Executing
Event-Handlers
Event occurs
App is ready
All Handlers executed
Properties bound
02.07.2016
7
Setting up Bindings
Page  18
View
Page  20
<button (click)="search()" [disabled]="!from || !to">
<table>
<tr *ngFor="let flight of flights">
<td>{{flight.id}}</td>
<td>{{flight.date}}</td>
<td>{{flight.from}}</td>
<td>{{flight.to}}</td>
<td><a href="#" (click)="selectFlight(flight)">Select</a></td>
</tr>
</table>
<td [text-content]="flight.id"></td>
02.07.2016
8
View
Page  21
<button on-click="search()" bind-disabled="!from || !to">
<table>
<tr *ngFor="let flight of flights">
<td>{{flight.id}}</td>
<td>{{flight.date}}</td>
<td>{{flight.from}}</td>
<td>{{flight.to}}</td>
<td><a href="#" on-click="selectFlight(flight)">Select</a></td>
</tr>
</table>
<td [text-content]="flight.id"></td>
Recap
• Property-Binding: One-Way; Top/Down
• Event-Binding: One-Way; Bottom/Up
• Two-Way-Binding?
• Two-Way = Property-Binding + Event-Binding
Page  22
02.07.2016
9
Combining Property- and Event-Bindings
Page  23
<input [ngModel]="from" (ngModelChange)="updateFrom($event)">
updateFrom(newValue) {
this.from = newValue;
}
Combining Property- and Event-Bindings
Page  24
<input [ngModel]="from" (ngModelChange)="from = $event">
02.07.2016
10
Syntax-Sugar for „simulating“ Two-Way
Page  25
<input [(ngModel)]="from">
<input bindon-ngModel="from">
Performance-Tuning with
Immutables and Observables
02.07.2016
11
Angular traverses the whole tree by default
flights
flight flight
{{ flight.id }} {{ flight.id }}
FlightSearch
Card Card
Immutables
• Objects that can not change
• When represented data changes, they have been replaced by
an other immutable
• Easy to find out if there was a change
• oldObject == newObject
• With or without libs (like immutable.js)
02.07.2016
12
Immutables
const ONE_MINUTE = 1000 * 60;
let oldFlights = this.flights;
let oldFlight = oldFlights[0]; // Flight to change!
let oldFlightDate = new Date(oldFlight.date); // Date to change
Immutables
let newFlightDate = new Date(oldFlightDate.getTime() + ONE_MINUTE * 15);
let newFlight = {
id: oldFlight.id,
from: oldFlight.from,
to: oldFlight.to,
date: newFlightDate.toISOString()
};
let newFlights = [
newFlight,
...oldFlights.slice(1, this.flights.length)
];
this.flights = newFlights;
02.07.2016
13
Checking for Change
console.debug("Array: " + (oldFlights == newFlights)); // false
console.debug("#0: " + (oldFlights[0] == newFlights[0])); // false
console.debug("#1: " + (oldFlights[1] == newFlights[1])); // true
Immutables and Angular 2
• Data flows top/down
• When Angular 2 assumes that every @Input of a component
is an immutable it just has to check if they changed
• If not: Skip component and every child component (whole
sub-tree)
02.07.2016
14
Immutables and Angular
flights
flight flight
{{ flight.id }} {{ flight.id }}
FlightSearch
Card Card
Change
How to tell Angular to assume Immutables?
@Component({
[…]
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FlightCard {
[…]
@Input flight;
}
02.07.2016
15
DEMO
Observables mit OnPush
Page  41
flights
flight$ flight$
{{ flight$.id }} {{ flight$.id }}
FlightSearch
Card Card
Change
02.07.2016
16
Observables mit OnPush
Page  42
flights$
flight flight
{{ flight.id }} {{ flight.id }}
FlightSearch
Card
Change
Card
How to bind to an Observable?
<flight-card
[item]="flight | async" […]>
</flight-card>
02.07.2016
17
DEMO
Not "all-or-nothing"
• Using Immutables and Obersvables isn't an "all-or-nothing-
thing"
• You can just use for componentes that need additional
performance
02.07.2016
18
Contact
manfred.steyer@SOFTWAREarchitekt.at
SOFTWAREarchitekt.at
ManfredSteyer

More Related Content

What's hot (20)

PPTX
Routing And Navigation
Eyal Vardi
 
PPTX
Angular 2.0 Dependency injection
Eyal Vardi
 
PPTX
AngularJS $Provide Service
Eyal Vardi
 
PPTX
AngularJS Internal
Eyal Vardi
 
PPTX
Angular 2.0 Pipes
Eyal Vardi
 
PPTX
AngularJs $provide API internals & circular dependency problem.
Yan Yankowski
 
PPTX
Angular 2.0 Routing and Navigation
Eyal Vardi
 
PDF
준비하세요 Angular js 2.0
Jeado Ko
 
PDF
AngularJS Framework
Barcamp Saigon
 
PDF
AngularJS Basics with Example
Sergey Bolshchikov
 
PPTX
AngularJs
syam kumar kk
 
PPTX
Building complex User Interfaces with Sitecore and React
Jonne Kats
 
PDF
[FEConf Korea 2017]Angular 컴포넌트 대화법
Jeado Ko
 
PPTX
Practical AngularJS
Wei Ru
 
PPTX
AngularJS Architecture
Eyal Vardi
 
PPT
Backbone js
husnara mohammad
 
PDF
Get AngularJS Started!
Dzmitry Ivashutsin
 
PDF
AngularJS Basic Training
Cornel Stefanache
 
PPTX
AngularJS Directives
Eyal Vardi
 
PPTX
AngularJS in 60ish Minutes
Dan Wahlin
 
Routing And Navigation
Eyal Vardi
 
Angular 2.0 Dependency injection
Eyal Vardi
 
AngularJS $Provide Service
Eyal Vardi
 
AngularJS Internal
Eyal Vardi
 
Angular 2.0 Pipes
Eyal Vardi
 
AngularJs $provide API internals & circular dependency problem.
Yan Yankowski
 
Angular 2.0 Routing and Navigation
Eyal Vardi
 
준비하세요 Angular js 2.0
Jeado Ko
 
AngularJS Framework
Barcamp Saigon
 
AngularJS Basics with Example
Sergey Bolshchikov
 
AngularJs
syam kumar kk
 
Building complex User Interfaces with Sitecore and React
Jonne Kats
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
Jeado Ko
 
Practical AngularJS
Wei Ru
 
AngularJS Architecture
Eyal Vardi
 
Backbone js
husnara mohammad
 
Get AngularJS Started!
Dzmitry Ivashutsin
 
AngularJS Basic Training
Cornel Stefanache
 
AngularJS Directives
Eyal Vardi
 
AngularJS in 60ish Minutes
Dan Wahlin
 

Similar to Databinding and Performance-Tuning in Angular 2 (20)

PDF
Data Binding and Forms in Angular 2
Manfred Steyer
 
PPTX
Angular 2.0 change detection
Ran Wahle
 
PDF
20 000 Leagues Under The Angular 4
Oleksandr Tryshchenko
 
PDF
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
JSC “Arcadia Inc”
 
PDF
New Angular project and created a component.
Ritika
 
PDF
Angular change detection - Dakar Promise JS meetup
Cheikh Omar SOW
 
PDF
Instant download Angular 2 Cookbook Frisbie pdf all chapter
ikraanfalan
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PDF
Angular data binding
Sultan Ahmed
 
PDF
Tech Webinar: Angular 2, Introduction to a new framework
Codemotion
 
PDF
Exploring Angular 2 - Episode 2
Ahmed Moawad
 
PDF
Data models in Angular 1 & 2
Adam Klein
 
PPTX
Client Best Practices
Yuval Dagai
 
PDF
Immutability, and how to do it in JavaScripts
Anton Astashov
 
PDF
Reactive programming in Angular 2
Yakov Fain
 
PPTX
yrs of IT experience in enterprise programming
narasimhulum1623
 
PDF
Angular Data
Stefan Unterhofer
 
PPTX
Angular 2 KTS
John Vall
 
PDF
What is data binding in Angular
technicalChamber
 
PDF
Angular 2 - Core Concepts
Fabio Biondi
 
Data Binding and Forms in Angular 2
Manfred Steyer
 
Angular 2.0 change detection
Ran Wahle
 
20 000 Leagues Under The Angular 4
Oleksandr Tryshchenko
 
AzovDevMeetup 2016 | Angular 2: обзор | Александр Шевнин
JSC “Arcadia Inc”
 
New Angular project and created a component.
Ritika
 
Angular change detection - Dakar Promise JS meetup
Cheikh Omar SOW
 
Instant download Angular 2 Cookbook Frisbie pdf all chapter
ikraanfalan
 
Angular Data Binding
Jennifer Estrada
 
Angular data binding
Sultan Ahmed
 
Tech Webinar: Angular 2, Introduction to a new framework
Codemotion
 
Exploring Angular 2 - Episode 2
Ahmed Moawad
 
Data models in Angular 1 & 2
Adam Klein
 
Client Best Practices
Yuval Dagai
 
Immutability, and how to do it in JavaScripts
Anton Astashov
 
Reactive programming in Angular 2
Yakov Fain
 
yrs of IT experience in enterprise programming
narasimhulum1623
 
Angular Data
Stefan Unterhofer
 
Angular 2 KTS
John Vall
 
What is data binding in Angular
technicalChamber
 
Angular 2 - Core Concepts
Fabio Biondi
 
Ad

More from Manfred Steyer (20)

PDF
Der neue Component Router für Angular 2
Manfred Steyer
 
PDF
Datenbindung und Performance in Angular 2
Manfred Steyer
 
PDF
Single Page Applications neu gedacht: Redux in Angular 2 mit @ngrx/store
Manfred Steyer
 
PDF
Offlinefähige Browseranwendungen: Progressive Web-Apps mit Angular 2
Manfred Steyer
 
PDF
Angular 2: Die Ideen hinter Datenbindung und Formularen im Detail betrachtet
Manfred Steyer
 
PDF
Datengetriebene Web APIs mit Entity Framework
Manfred Steyer
 
PDF
Angular 2 Upgrade: Migration von AngularJS 1.x zu 2.0
Manfred Steyer
 
PDF
Web APIs mit ASP.NET Core 1
Manfred Steyer
 
PDF
The newst new Router for Angular 2 ("Version 3")
Manfred Steyer
 
PDF
Modern authentication solutions in Angular 2 with OAuth 2.0 and OpenId Connect
Manfred Steyer
 
PDF
Progressive web apps with Angular 2
Manfred Steyer
 
PDF
Der neueste neue Router (Version 3) für Angular 2
Manfred Steyer
 
PDF
Webpack
Manfred Steyer
 
PDF
ASP.NET Core 1 for MVC- and WebAPI-Devs
Manfred Steyer
 
PDF
EF Core 1: News features and changes
Manfred Steyer
 
PDF
Angular 2: Migration - SSD 2016 London
Manfred Steyer
 
PDF
Angular 2 - SSD 2016 London
Manfred Steyer
 
PDF
ASP.NET Web API Deep Dive - SSD 2016 London
Manfred Steyer
 
PDF
Was bringt Angular 2?
Manfred Steyer
 
PDF
Web APIs mit ASP.NET MVC Core 1
Manfred Steyer
 
Der neue Component Router für Angular 2
Manfred Steyer
 
Datenbindung und Performance in Angular 2
Manfred Steyer
 
Single Page Applications neu gedacht: Redux in Angular 2 mit @ngrx/store
Manfred Steyer
 
Offlinefähige Browseranwendungen: Progressive Web-Apps mit Angular 2
Manfred Steyer
 
Angular 2: Die Ideen hinter Datenbindung und Formularen im Detail betrachtet
Manfred Steyer
 
Datengetriebene Web APIs mit Entity Framework
Manfred Steyer
 
Angular 2 Upgrade: Migration von AngularJS 1.x zu 2.0
Manfred Steyer
 
Web APIs mit ASP.NET Core 1
Manfred Steyer
 
The newst new Router for Angular 2 ("Version 3")
Manfred Steyer
 
Modern authentication solutions in Angular 2 with OAuth 2.0 and OpenId Connect
Manfred Steyer
 
Progressive web apps with Angular 2
Manfred Steyer
 
Der neueste neue Router (Version 3) für Angular 2
Manfred Steyer
 
ASP.NET Core 1 for MVC- and WebAPI-Devs
Manfred Steyer
 
EF Core 1: News features and changes
Manfred Steyer
 
Angular 2: Migration - SSD 2016 London
Manfred Steyer
 
Angular 2 - SSD 2016 London
Manfred Steyer
 
ASP.NET Web API Deep Dive - SSD 2016 London
Manfred Steyer
 
Was bringt Angular 2?
Manfred Steyer
 
Web APIs mit ASP.NET MVC Core 1
Manfred Steyer
 
Ad

Recently uploaded (20)

PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
PPTX
Academic Debate: Creation vs Evolution.pptx
JOHNPATRICKMARTINEZ5
 
PDF
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PPTX
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
PPTX
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
The Convergence of Threat Behaviors Across Intrusions
Joe Slowik
 
PDF
Cybersecurity Nightmare_ 16 Billion Passwords Leaked in Data Breach by Orage ...
Orage Technologies
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
PDF
Learning Exemplar_Technology and Livelihood Education 7 Q1_W2.pdf
mjhiludo16
 
PDF
web application development company in bangalore.pdf
https://dkpractice.co.in/seo.html tech
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PPTX
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
PPTX
My Mother At 66! (2).pptx00000000000000000000000000000
vedapattisiddharth
 
PPTX
CHAPTER 1 - PART 3 FOR GRADE 11 STUDENTS
FSBTLEDNathanVince
 
PDF
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
PDF
google promotion services in Delhi, India
Digital Web Future
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
Academic Debate: Creation vs Evolution.pptx
JOHNPATRICKMARTINEZ5
 
The Hidden Benefits of Outsourcing IT Hardware Procurement for Small Businesses
Carley Cramer
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
美国电子毕业证帕克大学电子版成绩单UMCP学费发票办理学历认证
Taqyea
 
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
The Convergence of Threat Behaviors Across Intrusions
Joe Slowik
 
Cybersecurity Nightmare_ 16 Billion Passwords Leaked in Data Breach by Orage ...
Orage Technologies
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
Learning Exemplar_Technology and Livelihood Education 7 Q1_W2.pdf
mjhiludo16
 
web application development company in bangalore.pdf
https://dkpractice.co.in/seo.html tech
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
原版一样(毕业证书)法国蒙彼利埃大学毕业证文凭复刻
Taqyea
 
My Mother At 66! (2).pptx00000000000000000000000000000
vedapattisiddharth
 
CHAPTER 1 - PART 3 FOR GRADE 11 STUDENTS
FSBTLEDNathanVince
 
Empowering Local Language Email with IDN & EAI – Powered by XgenPlus
XgenPlus Technologies
 
google promotion services in Delhi, India
Digital Web Future
 

Databinding and Performance-Tuning in Angular 2

  • 1. 02.07.2016 1 Databinding and Performance-Tuning in Angular 2 Manfred Steyer Contents • How databinding works • How to improve performance with Immutables and Observables
  • 2. 02.07.2016 2 About me … • Manfred Steyer • SOFTWAREarchitekt.at • Trainer & Consultant • Focus: Angular Page  3 Manfred Steyer Bindings in Angular 2 Page  4
  • 3. 02.07.2016 3 Some Goals of Angular 2 Page  5 Performance Components Predictability Data-Binding in Angular 1.x Page  6 Model Model Directive Nearly everything can depend on everything Solution: Multiple Digest-Cycles
  • 4. 02.07.2016 4 Component-Tree in Angular 2 Page  7 Component for whole App Component (e. g. list) Component (e. g. list-item) Component (e. g. list-item) Rules for Property-Bindings • A Component only depends on its own data (and indirectly on its parents data) • A Component must not depend on its children data! • Dependency Graph is a tree • Angular only needs one iteration („digest“) to update tree Page  9
  • 5. 02.07.2016 5 Property-Binding Page  10 model item item {{ item.title }} {{ item.title }} [http://victorsavkin.com/post/110170125256/change-detection-in-angular-2] Event-Bindings (One-Way, Bottom/Up) Page  14 {{ item.title }} {{ item.title }} Event-Handler Event-Handler
  • 6. 02.07.2016 6 Event-Bindings (One-Way, Bottom/Up) • No Digest-Iterations for propagating events • But: Events can change state  Digest for updating Property-Bindings Page  15 Property- and Event-Bindings Page  17 Performing Property-Binding Executing Event-Handlers Event occurs App is ready All Handlers executed Properties bound
  • 7. 02.07.2016 7 Setting up Bindings Page  18 View Page  20 <button (click)="search()" [disabled]="!from || !to"> <table> <tr *ngFor="let flight of flights"> <td>{{flight.id}}</td> <td>{{flight.date}}</td> <td>{{flight.from}}</td> <td>{{flight.to}}</td> <td><a href="#" (click)="selectFlight(flight)">Select</a></td> </tr> </table> <td [text-content]="flight.id"></td>
  • 8. 02.07.2016 8 View Page  21 <button on-click="search()" bind-disabled="!from || !to"> <table> <tr *ngFor="let flight of flights"> <td>{{flight.id}}</td> <td>{{flight.date}}</td> <td>{{flight.from}}</td> <td>{{flight.to}}</td> <td><a href="#" on-click="selectFlight(flight)">Select</a></td> </tr> </table> <td [text-content]="flight.id"></td> Recap • Property-Binding: One-Way; Top/Down • Event-Binding: One-Way; Bottom/Up • Two-Way-Binding? • Two-Way = Property-Binding + Event-Binding Page  22
  • 9. 02.07.2016 9 Combining Property- and Event-Bindings Page  23 <input [ngModel]="from" (ngModelChange)="updateFrom($event)"> updateFrom(newValue) { this.from = newValue; } Combining Property- and Event-Bindings Page  24 <input [ngModel]="from" (ngModelChange)="from = $event">
  • 10. 02.07.2016 10 Syntax-Sugar for „simulating“ Two-Way Page  25 <input [(ngModel)]="from"> <input bindon-ngModel="from"> Performance-Tuning with Immutables and Observables
  • 11. 02.07.2016 11 Angular traverses the whole tree by default flights flight flight {{ flight.id }} {{ flight.id }} FlightSearch Card Card Immutables • Objects that can not change • When represented data changes, they have been replaced by an other immutable • Easy to find out if there was a change • oldObject == newObject • With or without libs (like immutable.js)
  • 12. 02.07.2016 12 Immutables const ONE_MINUTE = 1000 * 60; let oldFlights = this.flights; let oldFlight = oldFlights[0]; // Flight to change! let oldFlightDate = new Date(oldFlight.date); // Date to change Immutables let newFlightDate = new Date(oldFlightDate.getTime() + ONE_MINUTE * 15); let newFlight = { id: oldFlight.id, from: oldFlight.from, to: oldFlight.to, date: newFlightDate.toISOString() }; let newFlights = [ newFlight, ...oldFlights.slice(1, this.flights.length) ]; this.flights = newFlights;
  • 13. 02.07.2016 13 Checking for Change console.debug("Array: " + (oldFlights == newFlights)); // false console.debug("#0: " + (oldFlights[0] == newFlights[0])); // false console.debug("#1: " + (oldFlights[1] == newFlights[1])); // true Immutables and Angular 2 • Data flows top/down • When Angular 2 assumes that every @Input of a component is an immutable it just has to check if they changed • If not: Skip component and every child component (whole sub-tree)
  • 14. 02.07.2016 14 Immutables and Angular flights flight flight {{ flight.id }} {{ flight.id }} FlightSearch Card Card Change How to tell Angular to assume Immutables? @Component({ […] changeDetection: ChangeDetectionStrategy.OnPush }) export class FlightCard { […] @Input flight; }
  • 15. 02.07.2016 15 DEMO Observables mit OnPush Page  41 flights flight$ flight$ {{ flight$.id }} {{ flight$.id }} FlightSearch Card Card Change
  • 16. 02.07.2016 16 Observables mit OnPush Page  42 flights$ flight flight {{ flight.id }} {{ flight.id }} FlightSearch Card Change Card How to bind to an Observable? <flight-card [item]="flight | async" […]> </flight-card>
  • 17. 02.07.2016 17 DEMO Not "all-or-nothing" • Using Immutables and Obersvables isn't an "all-or-nothing- thing" • You can just use for componentes that need additional performance