Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Angular Performance 101
Search
Aviv Ben-Yosef
June 17, 2014
Programming
1
18k
Angular Performance 101
From JS-IL 2014, given on June 17, 2014.
Aviv Ben-Yosef
June 17, 2014
Tweet
Share
More Decks by Aviv Ben-Yosef
See All by Aviv Ben-Yosef
HTTP API Design: UX for the Nitpickeriest
avivby
0
53
Stuff You Didn't Know You Can Do with RSpec
avivby
1
3.1k
Introduction to Rubinius
avivby
1
4k
DRY Introduction
avivby
1
280
How BillGuard Does MongoDB
avivby
5
3.2k
Other Decks in Programming
See All in Programming
Deep Dive into Kotlin Flow
jmatsu
1
360
速いWebフレームワークを作る
yusukebe
5
1.7k
ファインディ株式会社におけるMCP活用とサービス開発
starfish719
0
2k
奥深くて厄介な「改行」と仲良くなる20分
oguemon
1
560
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
今だからこそ入門する Server-Sent Events (SSE)
nearme_tech
PRO
3
250
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
560
Testing Trophyは叫ばない
toms74209200
0
890
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
4
2.9k
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
480
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
2.5k
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
340
Featured
See All Featured
A designer walks into a library…
pauljervisheath
207
24k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
How STYLIGHT went responsive
nonsquared
100
5.8k
Large-scale JavaScript Application Architecture
addyosmani
513
110k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Docker and Python
trallard
46
3.6k
It's Worth the Effort
3n
187
28k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Transcript
Angular Performance 101
Disclaimer
“Premature optimization is the root of all Evil”
A story
None
<div ng-repeat=“task in tasks”> <span> {{ task.title }} </span> <span>
{{ task.date | fromNow }} </span> </div>
Why did it keep updating?
None
$interval(function() { $http.get(‘/events’) .success(function(events) { if (events) { $scope.events =
events; } }); }, 20 * 1000);
Why?
Angular == SO. MUCH. MAGIC.
“With great power comes great responsibility”
We’ll learn How dirty checking is performed When it runs
What you should do
The Secret
Magic == Dirty checking
How dirty checking is performed
Digest Loop
Digest == Run all watch expressions
$scope.$watch()
… and {{ stuff }}
… and ng-show=“foo()”
… and isolated scope variables scope: {foo: ‘=’}
… and {{ value | myFilter }}
… and ng-repeat itself
Watch Expressions $scope.$watch() {{ stuff }} ng-show=“list.length > 0” isolated
scope bindings: {foo: ‘=’} filters: {{ date | fromNow }} ng-repeat Basically everything
When does it run?
User actions
ng-click
ng-change
ng-model
… and $http responses
… and when $q promises are resolved
… and when using $timeout or $interval
… and whenever you call $scope.$apply() or $scope.$digest()
When digest runs User actions (ng-click, ng-model, etc.) $http responses
$q promises resolved $timeout / $interval $scope.$apply() / $scope.$digest() Basically: a lot of things
So? Who cares?
None
But when the shit hits the fan
You click and the screen freezes for a while The
only* performance issue that is angular’s fault
Are all clicks slow? Or just a certain screen?
One Screen is Slow That screen has bad scopes: Too
many watches Watch handlers that take too long
Everything is Slow Your digest loop is taking too long
You’re doing it wrong
Pinpoint it
Batarang
See watch expression
See slow expressions
Time Your $digest console.time(‘digest’); $rootScope.$digest(); console.timeEnd(‘digest’); http://jsfiddle.net/mbRf8/
What you should do
Make watches faster
Making Watches Faster Switch deep watch with $watchCollection $scope.$watch(‘myList’, function()
{}, true); ! $scope.$watchCollection(‘myList’, function() {});
Making Watches Faster Deep watch a subset $scope.$watch(‘myList’, function() {},
true); ! $scope.$watch( function() {return _.pluck($scope.myList, ‘id’);}, function() {}, true);
Making Watches Faster Make the change handler more performant
Have less watches
ng-repeat is your enemy
ng-repeat is your enemy Use track by <div ng-repeat=“task in
tasks track by task.id”> http://bit.ly/1kyvet7
ng-repeat is your enemy Paginate <div ng-repeat=“task in tasks”> !
<div ng-repeat=“task in tasksPage”>
ng-repeat is your enemy Row Virtualization à-la ng-grid http://angular-ui.github.io/ng-grid/
ng-repeat is your enemy Bind Once https://github.com/Pasvaz/bindonce <div bindonce ng-repeat=“task
in tasks”> ! <span bo-text=“task.title”></span> ! </div>
Have Less Watches ng-hide & ng-show elements are still there,
consider using ng-if Consider replacing filters with pre-calculated values
Badass AKA “I’m Desperate”
Badass Mode Write an angular-less widget
Your Mission Pinpoint bad watches Make watches faster Use less
watches
Thank You! Aviv Ben-Yosef @avivby www.deflect.io ! My angular &
web development newsletter www.codelord.net