我最近一直在弄乱 Batarang 插件来分析一些性能。我注意到在每个日志的顶部都有一个专门用于称为regularInterceptedExpression 的部分。任何人都可以解释这意味着什么以及提高性能的一些方法。我在某处读到可能来自在指令中使用“=”属性的地方。如果其他人看到这个,有没有解决方案?
1 回答
如果您深入研究 AngularJS 代码,您可以看到regularInterceptedExpression(scope, locals, assign, inputs)
在 function 中定义的函数addInterceptor(parsedExpression, interceptorFn)
。唯一使用函数的地方addInterceptor(parsedExpression, interceptorFn)
是函数$parse(exp, interceptorFn, expensiveChecks)
。这是字符串和其他手表转换为函数的地方。您需要将angular.js
文件更新为
1)增强$parse(exp, interceptorFn, expensiveChecks)
保留解析源的功能:
$$source
通过将 设置为函数的第一个参数来查找方法的结尾和每个 switch case 结束更新addInterceptor
。
parsedExpression.$$source = exp; // keep the source expression handy
return addInterceptor(parsedExpression, interceptorFn);
case 'function':
exp.$$source = exp; // keep the source expression handy
return addInterceptor(exp, interceptorFn);
default:
noop.$$source = exp; // keep the source expression handy
return addInterceptor(noop, interceptorFn);
2)在regularInterceptedExpression
函数内部收集对该函数的调用的统计信息:
var fn = regularWatch ? function regularInterceptedExpression(scope, locals, assign, inputs) {
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
window.$$rieStats = window.$$rieStats || {};
window.$$rieStats[parsedExpression.$$source] = (window.$$rieStats[parsedExpression.$$source] ? window.$$rieStats[parsedExpression.$$source] : 0) + 1;
return interceptorFn(value, scope, locals);
3) 运行您的应用程序并检查统计信息,即打开开发工具并写入$$rieStats
JavaScript 控制台。您应该看到该regularInterceptedExpression
函数调用的观察者数量。
Object.keys($$rieStats).sort(function(a,b){return $$rieStats[a]-$$rieStats[b]}).reverse().forEach(function(item){ console.log(item, $$rieStats[item])})
提示:您还可以将$$rieStats
计数添加到另一个分支函数oneTimeInterceptedExpression
以跟踪一次绑定。