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

perf(ngModel,form): avoid invoking $interpolate for empty/unset values #10414

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions benchmarks/largetable-bp/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<div>interpolation + fnInvocation: <input type="radio" ng-model="benchmarkType" value="interpolationFn"></div>
<div>ngBind + filter: <input type="radio" ng-model="benchmarkType" value="ngBindFilter"></div>
<div>interpolation + filter: <input type="radio" ng-model="benchmarkType" value="interpolationFilter"></div>
<div>ngModel: <input type="radio" ng-model="benchmarkType" value="ngModel"></div>

<ng-switch on="benchmarkType">
<baseline-binding-table ng-switch-when="baselineBinding">
Expand Down Expand Up @@ -77,6 +78,13 @@ <h2>interpolation with filter</h2>
<span ng-repeat="column in row">{{column.i | noop}}:{{column.j | noop}}|</span>
</div>
</div>
<div ng-switch-when="ngModel">
<h2>ngModels</h2>
<div ng-repeat="row in data">
<input type="text" ng-model="row.i" name="constName" />
<input type="text" ng-model="row.j" />
</div>
</div>
</ng-switch>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ function $InterpolateProvider() {
return '\\\\\\' + ch;
}

//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) {
return constantInterp(scope);
}, function constantInterpolateListener(value, old, scope) {
if (isFunction(listener)) {
listener.apply(this, arguments);
}
unwatch();
}, objectEquality);
}

/**
* @ngdoc service
* @name $interpolate
Expand Down Expand Up @@ -183,6 +196,19 @@ 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 = function constantInterpolationFn() { return unescapedText; };
constantInterp.exp = text;
constantInterp.expressions = [];
constantInterp.$$watchDelegate = constantWatchDelegate;
}
return constantInterp;
}

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

expect($rootScope.$countWatchers()).toBe(0);
}));

it('should stop watching strings with no expressions after first execution',
inject(function($interpolate, $rootScope) {
var spy = jasmine.createSpy();
$rootScope.$watch($interpolate('foo'), spy);
$rootScope.$digest();
expect(($rootScope.$$watchers || []).length).toBe(0);
expect(spy).toHaveBeenCalledWith('foo', 'foo', $rootScope);
expect(spy.calls.length).toBe(1);
})
);

it('should stop watching strings with only constant expressions after first execution',
inject(function($interpolate, $rootScope) {
var spy = jasmine.createSpy();
$rootScope.$watch($interpolate('foo {{42}}'), spy);
$rootScope.$digest();
expect(($rootScope.$$watchers || []).length).toBe(0);
expect(spy).toHaveBeenCalledWith('foo 42', 'foo 42', $rootScope);
expect(spy.calls.length).toBe(1);
})
);
});

describe('interpolation escaping', function() {
Expand All @@ -135,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