Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ece6ef4

Browse files
committedAug 19, 2014
perf($parse): optimize filter implementation
- generate less garbage - optimize argument collection - use call instead of apply for no arg case (2x boost)
1 parent f7fde04 commit ece6ef4

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed
 

‎src/ng/parse.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -550,19 +550,31 @@ Parser.prototype = {
550550
filter: function() {
551551
var token = this.expect();
552552
var fn = this.$filter(token.text);
553-
var argsFn = [];
554-
while(this.expect(':')) {
555-
argsFn.push(this.expression());
556-
}
557-
return valueFn(fnInvoke);
553+
var argsFn;
554+
var args;
558555

559-
function fnInvoke(self, locals, input) {
560-
var args = [input];
561-
for (var i = 0; i < argsFn.length; i++) {
562-
args.push(argsFn[i](self, locals));
556+
if (this.peek(':')) {
557+
argsFn = [];
558+
args = []; // we can safely reuse the array
559+
while (this.expect(':')) {
560+
argsFn.push(this.expression());
563561
}
564-
return fn.apply(self, args);
565562
}
563+
564+
return valueFn(function $parseFilter(self, locals, input) {
565+
if (args) {
566+
args[0] = input;
567+
568+
var i = argsFn.length;
569+
while (i--) {
570+
args[i + 1] = argsFn[i](self, locals);
571+
}
572+
573+
return fn.apply(self, args);
574+
}
575+
576+
return fn.call(self, input);
577+
});
566578
},
567579

568580
expression: function() {

0 commit comments

Comments
 (0)
This repository has been archived.