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

refactor($interpolate): optimize watched $interpolate functions performance #4556

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor($interpolation): small attempt to add clarity
  • Loading branch information
rodyhaddad committed Jan 10, 2014
commit 8386ff6e0c453fb8f2e58fb1f2a03798d3a4ea78
24 changes: 13 additions & 11 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ function $InterpolateProvider() {
forEach(parts, function(value, index) {
if (isFunction(value)) {
expressions[index] = value;
concat[index] = '';
} else {
concat[index] = value;
}
Expand Down Expand Up @@ -214,8 +213,7 @@ function $InterpolateProvider() {
};

fn = function(scope) {
// we don't want others to be able to pass more than the first argument
return getConcatValue(scope);
return getConcatValue(scope /*, undefined, ...*/);
};
fn.exp = text;
fn.parts = parts;
Expand All @@ -225,26 +223,30 @@ function $InterpolateProvider() {
var lastTextValue, lastValues = {}, watchersRm = [];

forEach(expressions, function(expression, index) {
watchersRm.push(scope.$watch(function watchInterpolatedExpr(scope) {
watchersRm.push(
scope.$watch(watcherOf(expression), listenerOf(index), objectEquality));
});

function watcherOf(expression) {
return function interpolatedExprWatcher(scope) {
try {
return getStringValue(expression(scope));
} catch (err) {
var newErr = $interpolateMinErr('interr', "Can't interpolate: {0}\n{1}",
text, err.toString());
$exceptionHandler(newErr);
}
}, listenerOf(index), objectEquality));
});
}
}

function listenerOf(index) {
return function interpolatedExprListener(value, oldValue) {
// we only invoke the origListener if the current value
// is not equal to the last computed value
// ex: if in `{{a}}-{{b}}` both values change in a digest,
// the listener of `a` gets invoked first, we compute the string
// and invoke the origListener once,
// and ignore it when the listener of `b` gets triggered
// (unless the value of `b` changes again since the last computation)
// ex: if in `{{a}}-{{b}}` both values change in a digest, the listener of
// `a` gets invoked first, we compute the string and call the origListener
// and don't invoke it again when the listener of `b` gets triggered
// (unless the value of `b` changes again since the last computation!)
if (value !== lastValues[index]) {
var textValue = getConcatValue(scope, index, value, lastValues);
origListener.call(this, textValue,
Expand Down