angular-filter icon indicating copy to clipboard operation
angular-filter copied to clipboard

Cache no longer works (angular >= 1.3)

Open barzilaydn opened this issue 10 years ago • 1 comments

Hey all,

Because of this line from Angular 1.2:

return fn.apply(self, args); 

https://github.com/angular/angular.js/blob/g3_v1_2/src/ng/parse.js#L595

Was changed to this line in Angular 1.3:

return fn.apply(undefined, args);

https://github.com/angular/angular.js/blob/g3_v1_3/src/ng/parse.js#L568

this in all the filters is no longer linked to the scope that called the filter and therefor filterWatcher gets scope=undefined which causes isScope(scope) to always return false which in turn causes cleanStateless() to get called after every filter call, cleanStateless() cleans the cache.

The consequence is major performance issues and slowdown on every $digest cycle (which re-evaluates the filter output even though the input did not change).

barzilaydn avatar Oct 27 '15 15:10 barzilaydn

This is a valid concern. Since this no longer refers to the encapsulating scope, to get the scope, it would need to be passed on manually. Fortunately, this is very easy and can be done by simply passing this in as a parameter to the relevant filters (which is only two, actually, chunkBy and groupBy), e.g. { collection | groupBy:this:groupKey }.

The downside is it's a breaking change and so this library's version would probably need to be bumped to 1.x.x to reflect that, unless there's another way...? Why do we need the cache, shouldn't Angular be caching the result of these filters? They're not stateful, so that should be the case.

icdevin avatar Jan 31 '16 08:01 icdevin