http://www.alexkras.com/11-tips-to-improve-angularjs-performance/#watchers
function $apply(expr) {
try {
return $eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
$root.$digest();
}
}
If appropriate, using $scope.$digest() can lead to big savings
setTimeout(() => {
// Your code
$scope.$digest()
}, 0);
$timeout(() => {
// Your code
}, 0); // Optional, defaults to 0
Default: calls $apply() implicitly
Potential change:
// MyDirective.js: a (possibly 3rd party) directive that has a bidirectional binding
app.directive('MyDirective', function() {
return {
scope: {
name: '='
}
};
});
// app.html: if our app will never change the value, pass a one-time binding
<div my-directive name="::myName"></div>
https://docs.angularjs.org/guide/expression#one-time-binding
$scope.$watch('currentUser', (updated) => {
$log.log(`New user name is ${updated.name}`);
}, true);
Best to still just watch single prop:
$watch('obj.prop', listener);
$scope.$watchCollection('currentUser', (updated) => {
$log.log(`New user name is ${updated.name}`);
});
$watchCollection() compares:
$scope.$watch('currentUser._rId', (updated) => {
$log.log(`New user name is ${updated.name}`);
});
<input
type="text"
placeholder="Search"
ng-model="q"
ng-model-options="{debounce: {'default': 500, 'blur': 0 } }"
/>
<input
type="text"
ng-model="searchOrg"
typeahead="org as org.name for org in getOrgs($viewValue)"
typeahead-wait-ms="250"
/>
For typeaheads:
ng-if
ng-show / ng-hide
http://www.codelord.net/2014/04/15/improving-ng-repeat-performance-with-track-by/
<ul class="tasks">
<li ng-repeat="task in tasks">
{{task.id}}: {{task.title}}
</li>
</ul>
$scope.tasks = newTasksFromTheServer;
Solution: use `track by` and specify a prop:
<ul class="tasks">
<li ng-repeat="task in tasks track by task.id">
{{task.id}}: {{task.title}}
</li>
</ul>
Setup
<!-- public/index.html (can just adjust the built file) -->
<!-- Change this line: -->
<html class="no-js" ng-app="clPlatform.app" ng-strict-di>
<!-- To this (remove `ng-strict-di` attribute): -->
<html class="no-js" ng-app="clPlatform.app">
Currently:
// TodoCtrl.js
app.controller('TodoCtrl', function ($scope) {
$scope.input = 'ex. buy milk';
$scope.$watch('input', (newVal) => {
// Do something
};
});
// TodoCtrl.js
app.controller('TodoCtrl', function ($scope) {
const vm = this;
vm.input = 'ex. buy milk';
$scope.$watch('vm.name', (newVal) => {
// Do something
});
});
// todo.html
<div>
<input type="text" ng-model="input" />
</div>
// app.js
app.config(($stateProvider) => {
$stateProvider
.state('todo', {
...
controller: 'TodoCtrl',
controllerAs: 'vm',
...
});
});
// todo.html
<div>
<input type="text" ng-model="vm.input" />
</div>
Instead:
Faster | Angular 2.0 | Style/Linter | Because Reasons |
---|---|---|---|
app.directive('TodoDirective', function () {
return {
scope: {}, // Doesn't have to be isolate scope
bindToController: {
input: '='
}
controller: function () {
this.input = 'ex. buy milk';
},
controllerAs: 'vm',
...
};
});
Use ES6 classes and DI through static $inject for everything
Currently:
// MyController.js
const ctrlFn = ($log) => {
this.sayHello = function () {
$log.log('hello');
};
};
ctrlFn.$inject = ['$log'];
angular.module('MyApp').controller('MyController', ctrlFn);
// MyController.js
export default class MyController {
static $inject = ['$log'];
constructor($log) {
this.$log = $log;
}
sayHello() {
this.$log.log('hello');
}
}
// Following would go away in Angular 2.0
angular.module('MyApp').controller('MyController ', MyController);
Instead:
Faster | Angular 2.0 | Style/Linter | Because Reasons |
---|---|---|---|
// Service: exports a constructor that gets new'd
angular.module('MyApp')
.service('MyService', function () {
this.sayHello = function () {
console.log('hello');
};
});
// Factory: exports a function
angular.module('MyApp')
.factory('MyService', function () {
return {
sayHello: function () {
console.log('hello');
};
}
});
// MyService.js
export default class MyService {
// Alternative dependency injection style, to-be-discussed
// static $inject = ['$log'];
constructor($log) {
this.$log = $log;
}
sayHello() {
this.$log.log('hello');
}
}
// Following just goes away when we upgrade
angular.module('MyApp').service('MyService', ['$log', MyService]);
Faster | Angular 2.0 | Style/Linter | Because Reasons |
---|---|---|---|