Skip to content

Commit

Permalink
perf($interpolate): provide a simplified result for constant expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored and gkalpak committed Nov 23, 2015
1 parent f3bf5a6 commit b497940
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ function $InterpolateProvider() {
return value;
}

//TODO: this is the same as the constantWatchDelegate in parse.js
function constantWatchDelegate(scope, listener, objectEquality, constantInterp) {
var unwatch;
return unwatch = scope.$watch(function constantInterpolateWatch(scope) {
unwatch();
return constantInterp(scope);
}, listener, objectEquality);
}

/**
* @ngdoc service
* @name $interpolate
Expand Down Expand Up @@ -223,6 +232,18 @@ function $InterpolateProvider() {
* - `context`: evaluation context for all expressions embedded in the interpolated text
*/
function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {
// Provide a quick exit and simplified result function for text with no interpolation
if (!text.length || text.indexOf(startSymbol) === -1) {
var constantInterp;
if (!mustHaveExpression) {
var unescapedText = unescapeText(text);
constantInterp = valueFn(unescapedText);
constantInterp.expressions = [];
constantInterp.$$watchDelegate = constantWatchDelegate;
}
return constantInterp;
}

allOrNothing = !!allOrNothing;
var startIndex,
endIndex,
Expand Down
1 change: 1 addition & 0 deletions test/ng/interpolateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe('$interpolate', function() {


it('should support escaping interpolation signs', inject(function($interpolate) {
expect($interpolate('\\{\\{')(obj)).toBe('{{');
expect($interpolate('{{foo}} \\{\\{bar\\}\\}')(obj)).toBe('Hello {{bar}}');
expect($interpolate('\\{\\{foo\\}\\} {{bar}}')(obj)).toBe('{{foo}} World');
}));
Expand Down

0 comments on commit b497940

Please sign in to comment.