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

Commit

Permalink
perf($interpolate): provide a simplified result for constant expressions
Browse files Browse the repository at this point in the history
Closes: #10414
  • Loading branch information
jbedard authored and lgalfaso committed Nov 1, 2015
1 parent 15bfea8 commit cf83b4f
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 cf83b4f

Please sign in to comment.