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

Commit cf83b4f

Browse files
jbedardlgalfaso
authored andcommitted
perf($interpolate): provide a simplified result for constant expressions
Closes: #10414
1 parent 15bfea8 commit cf83b4f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: src/ng/interpolate.js

+21
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ function $InterpolateProvider() {
128128
return value;
129129
}
130130

131+
//TODO: this is the same as the constantWatchDelegate in parse.js
132+
function constantWatchDelegate(scope, listener, objectEquality, constantInterp) {
133+
var unwatch;
134+
return unwatch = scope.$watch(function constantInterpolateWatch(scope) {
135+
unwatch();
136+
return constantInterp(scope);
137+
}, listener, objectEquality);
138+
}
139+
131140
/**
132141
* @ngdoc service
133142
* @name $interpolate
@@ -223,6 +232,18 @@ function $InterpolateProvider() {
223232
* - `context`: evaluation context for all expressions embedded in the interpolated text
224233
*/
225234
function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {
235+
// Provide a quick exit and simplified result function for text with no interpolation
236+
if (!text.length || text.indexOf(startSymbol) === -1) {
237+
var constantInterp;
238+
if (!mustHaveExpression) {
239+
var unescapedText = unescapeText(text);
240+
constantInterp = valueFn(unescapedText);
241+
constantInterp.expressions = [];
242+
constantInterp.$$watchDelegate = constantWatchDelegate;
243+
}
244+
return constantInterp;
245+
}
246+
226247
allOrNothing = !!allOrNothing;
227248
var startIndex,
228249
endIndex,

Diff for: test/ng/interpolateSpec.js

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ describe('$interpolate', function() {
157157

158158

159159
it('should support escaping interpolation signs', inject(function($interpolate) {
160+
expect($interpolate('\\{\\{')(obj)).toBe('{{');
160161
expect($interpolate('{{foo}} \\{\\{bar\\}\\}')(obj)).toBe('Hello {{bar}}');
161162
expect($interpolate('\\{\\{foo\\}\\} {{bar}}')(obj)).toBe('{{foo}} World');
162163
}));

0 commit comments

Comments
 (0)