perf($parse): optimize filter implementation

- generate less garbage
- optimize argument collection
- use call instead of apply for no arg case (2x boost)
This commit is contained in:
Igor Minar
2014-08-03 16:00:25 -07:00
parent f7fde04160
commit ece6ef479c

View File

@@ -550,19 +550,31 @@ Parser.prototype = {
filter: function() {
var token = this.expect();
var fn = this.$filter(token.text);
var argsFn = [];
while(this.expect(':')) {
argsFn.push(this.expression());
}
return valueFn(fnInvoke);
var argsFn;
var args;
function fnInvoke(self, locals, input) {
var args = [input];
for (var i = 0; i < argsFn.length; i++) {
args.push(argsFn[i](self, locals));
if (this.peek(':')) {
argsFn = [];
args = []; // we can safely reuse the array
while (this.expect(':')) {
argsFn.push(this.expression());
}
return fn.apply(self, args);
}
return valueFn(function $parseFilter(self, locals, input) {
if (args) {
args[0] = input;
var i = argsFn.length;
while (i--) {
args[i + 1] = argsFn[i](self, locals);
}
return fn.apply(self, args);
}
return fn.call(self, input);
});
},
expression: function() {