Answer the question
In order to leave comments, you need to log in
Does using getter hurt application performance?
Is it good to use a getter in controllers, doesn't it start the digest cycle once again?
let services = new WeakMap;
class SomeController {
constructor(SomeService) {
services.set(this, SomeService);
}
get svcData(){
return services.get(this).getData();
}
}
angular.module('app').controller('SomeController', SomeController);
class SomeService {
constructor() {
}
getData(){
return [
...
...
]
}
}
angular.module('app').service('SomeService', SomeService);
Answer the question
In order to leave comments, you need to log in
Well, why should you?
If nothing happens there that triggers the digest, then no.
By the way, you can check it by manipulating $digest like this:
var oldDigest = $scope.$digest;
$scope.$digest = function() {
console.log('digest fired');
oldDigest.call(this);
};
The getter does not specifically call new $digest cycles.
In your example, for each call to the getter, a completely new array is formed, and it will fail the comparison by reference. In this case, this comparison occurs on each $digest cycle. That is, for each $digest cycle (in the absence of deep watch), watchers will be launched. This is the only risk, since you can only get rid of this through deep watch, but this thing is already seriously hitting performance.
Well, this is not to mention the fact that your service will twitch for each $digest cycle. Instead, it was worth pulling the service once and replacing the data in the controller only when necessary.
If we take modern approaches, then we generally have to take out all the receipt of the state from the controllers (components) outside, for example, to resolvers and map everything on the component property. There already getters/setters are well suited, but only those getters/setters that are at the language level and not "methods". It is easier to test, easier to maintain, and generally opens up a lot of room for further architectural bacchanalia.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question