From 0fc11a43f126a5e4d26f8a5ecd2d145a1c65a34e Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 2 Nov 2016 08:39:50 -0700 Subject: [PATCH] perf(core): use `array.push` / `array.pop` instead of `splice` if possible --- .../@angular/core/src/linker/view_container.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/@angular/core/src/linker/view_container.ts b/modules/@angular/core/src/linker/view_container.ts index 3fa44cb49f..fd9f87d2a3 100644 --- a/modules/@angular/core/src/linker/view_container.ts +++ b/modules/@angular/core/src/linker/view_container.ts @@ -104,7 +104,12 @@ export class ViewContainer { nestedViews = []; this.nestedViews = nestedViews; } - nestedViews.splice(viewIndex, 0, view); + // perf: array.push is faster than array.splice! + if (viewIndex >= nestedViews.length) { + nestedViews.push(view); + } else { + nestedViews.splice(viewIndex, 0, view); + } var refRenderNode: any /** TODO #9100 */; if (viewIndex > 0) { var prevView = nestedViews[viewIndex - 1]; @@ -119,7 +124,13 @@ export class ViewContainer { } detachView(viewIndex: number): AppView { - const view = this.nestedViews.splice(viewIndex, 1)[0]; + const view = this.nestedViews[viewIndex]; + // perf: array.pop is faster than array.splice! + if (viewIndex >= this.nestedViews.length - 1) { + this.nestedViews.pop(); + } else { + this.nestedViews.splice(viewIndex, 1); + } if (view.type === ViewType.COMPONENT) { throw new Error(`Component views can't be moved!`); }