Skip to content

Commit fdf4309

Browse files
vicbIgorMinar
authored andcommitted
perf(common): optimize NgSwitch default case
relates to #11297
1 parent af996ef commit fdf4309

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

modules/@angular/common/src/directives/ng_switch.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Directive, Host, Input, TemplateRef, ViewContainerRef} from '@angular/co
1010

1111
import {ListWrapper} from '../facade/collection';
1212

13-
const _CASE_DEFAULT = new Object();
13+
const _CASE_DEFAULT = {};
1414

1515
export class SwitchView {
1616
constructor(
@@ -53,8 +53,7 @@ export class SwitchView {
5353
* root elements.
5454
*
5555
* Elements within `NgSwitch` but outside of a `NgSwitchCase` or `NgSwitchDefault` directives will
56-
* be
57-
* preserved at the location.
56+
* be preserved at the location.
5857
*
5958
* The `ngSwitchCase` directive informs the parent `NgSwitch` of which view to display when the
6059
* expression is evaluated.
@@ -72,18 +71,23 @@ export class NgSwitch {
7271

7372
@Input()
7473
set ngSwitch(value: any) {
75-
// Empty the currently active ViewContainers
76-
this._emptyAllActiveViews();
77-
78-
// Add the ViewContainers matching the value (with a fallback to default)
79-
this._useDefault = false;
74+
// Set of views to display for this value
8075
let views = this._valueViews.get(value);
81-
if (!views) {
76+
77+
if (views) {
78+
this._useDefault = false;
79+
} else {
80+
// No view to display for the current value -> default case
81+
// Nothing to do if the default case was already active
82+
if (this._useDefault) {
83+
return;
84+
}
8285
this._useDefault = true;
83-
views = this._valueViews.get(_CASE_DEFAULT) || null;
86+
views = this._valueViews.get(_CASE_DEFAULT);
8487
}
85-
this._activateViews(views);
8688

89+
this._emptyAllActiveViews();
90+
this._activateViews(views);
8791
this._switchValue = value;
8892
}
8993

@@ -119,7 +123,7 @@ export class NgSwitch {
119123
this._activeViews = [];
120124
}
121125

122-
private _activateViews(views: SwitchView[]): void {
126+
private _activateViews(views?: SwitchView[]): void {
123127
if (views) {
124128
for (var i = 0; i < views.length; i++) {
125129
views[i].create();

modules/@angular/common/test/directives/ng_switch_spec.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ export function main() {
5050
detectChangesAndExpectText('when b');
5151
}));
5252

53-
// TODO(robwormald): deprecate and remove
54-
it('should switch amongst when values using switchCase', async(() => {
55-
const template = '<div>' +
56-
'<ul [ngSwitch]="switchValue">' +
57-
'<template ngSwitchCase="a"><li>when a</li></template>' +
58-
'<template ngSwitchCase="b"><li>when b</li></template>' +
59-
'</ul></div>';
60-
61-
fixture = createTestComponent(template);
62-
63-
detectChangesAndExpectText('');
64-
65-
getComponent().switchValue = 'a';
66-
detectChangesAndExpectText('when a');
67-
68-
getComponent().switchValue = 'b';
69-
detectChangesAndExpectText('when b');
70-
}));
71-
7253
it('should switch amongst when values with fallback to default', async(() => {
7354
const template = '<div>' +
7455
'<ul [ngSwitch]="switchValue">' +
@@ -84,6 +65,9 @@ export function main() {
8465

8566
getComponent().switchValue = 'b';
8667
detectChangesAndExpectText('when default');
68+
69+
getComponent().switchValue = 'c';
70+
detectChangesAndExpectText('when default');
8771
}));
8872

8973
it('should support multiple whens with the same value', async(() => {

modules/benchmarks/src/tree/ng2_switch/tree.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class TreeComponent {
1515
data: TreeNode = emptyTree;
1616
}
1717

18-
@NgModule({imports: [BrowserModule], bootstrap: [TreeComponent], declarations: [TreeComponent]})
18+
@NgModule({
19+
imports: [BrowserModule],
20+
bootstrap: [TreeComponent],
21+
declarations: [TreeComponent],
22+
})
1923
export class AppModule {
2024
}

0 commit comments

Comments
 (0)