From 0fedb57cb00b654dac436b08dba4250d766f54f2 Mon Sep 17 00:00:00 2001 From: Shavi Pathania Date: Mon, 13 Nov 2017 21:13:37 +0530 Subject: [PATCH] docs(aio): Clearing array with [] (#20369) (#20395) Clearing array with setting length to 0 replaced with [] for being short and marginally efficient. For reference: [] is turned into a sequence of around 15 machine instructions on x64 (if bump pointer allocation succeeds), whereas a.length=0 is a C++ runtime function call, which requires 10-100x as many instructions. Benedikt Meurer PR Close #20395 --- .../examples/lifecycle-hooks/src/app/after-content.component.ts | 2 +- .../examples/lifecycle-hooks/src/app/after-view.component.ts | 2 +- .../examples/lifecycle-hooks/src/app/counter.component.ts | 2 +- .../examples/lifecycle-hooks/src/app/do-check.component.ts | 2 +- aio/content/examples/lifecycle-hooks/src/app/logger.service.ts | 2 +- .../examples/lifecycle-hooks/src/app/on-changes.component.ts | 2 +- aio/content/examples/lifecycle-hooks/src/app/spy.component.ts | 2 +- aio/content/examples/toh-pt4/src/app/message.service.ts | 2 +- aio/content/examples/toh-pt5/src/app/message.service.ts | 2 +- aio/content/examples/toh-pt6/src/app/message.service.ts | 2 +- aio/content/examples/universal/src/app/message.service.ts | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/aio/content/examples/lifecycle-hooks/src/app/after-content.component.ts b/aio/content/examples/lifecycle-hooks/src/app/after-content.component.ts index 0754539617..5c9abd7a7d 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/after-content.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/after-content.component.ts @@ -108,7 +108,7 @@ export class AfterContentParentComponent { } reset() { - this.logs.length = 0; + this.logs = []; // quickly remove and reload AfterContentComponent which recreates it this.show = false; this.logger.tick_then(() => this.show = true); diff --git a/aio/content/examples/lifecycle-hooks/src/app/after-view.component.ts b/aio/content/examples/lifecycle-hooks/src/app/after-view.component.ts index 571878d03f..f376f8684e 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/after-view.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/after-view.component.ts @@ -110,7 +110,7 @@ export class AfterViewParentComponent { } reset() { - this.logs.length = 0; + this.logs = []; // quickly remove and reload AfterViewComponent which recreates it this.show = false; this.logger.tick_then(() => this.show = true); diff --git a/aio/content/examples/lifecycle-hooks/src/app/counter.component.ts b/aio/content/examples/lifecycle-hooks/src/app/counter.component.ts index 7ede5ac550..f9783b844e 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/counter.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/counter.component.ts @@ -27,7 +27,7 @@ export class MyCounterComponent implements OnChanges { // Empty the changeLog whenever counter goes to zero // hint: this is a way to respond programmatically to external value changes. if (this.counter === 0) { - this.changeLog.length = 0; + this.changeLog = []; } // A change to `counter` is the only change we care about diff --git a/aio/content/examples/lifecycle-hooks/src/app/do-check.component.ts b/aio/content/examples/lifecycle-hooks/src/app/do-check.component.ts index 6196dd8edd..d46b362ab0 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/do-check.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/do-check.component.ts @@ -68,7 +68,7 @@ export class DoCheckComponent implements DoCheck { reset() { this.changeDetected = true; - this.changeLog.length = 0; + this.changeLog = []; } } diff --git a/aio/content/examples/lifecycle-hooks/src/app/logger.service.ts b/aio/content/examples/lifecycle-hooks/src/app/logger.service.ts index 56ddaf9063..312b785707 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/logger.service.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/logger.service.ts @@ -18,7 +18,7 @@ export class LoggerService { } } - clear() { this.logs.length = 0; } + clear() { this.logs = []; } // schedules a view refresh to ensure display catches up tick() { this.tick_then(() => { }); } diff --git a/aio/content/examples/lifecycle-hooks/src/app/on-changes.component.ts b/aio/content/examples/lifecycle-hooks/src/app/on-changes.component.ts index a5a49a61e9..e580fffd39 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/on-changes.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/on-changes.component.ts @@ -43,7 +43,7 @@ export class OnChangesComponent implements OnChanges { } // #enddocregion ng-on-changes - reset() { this.changeLog.length = 0; } + reset() { this.changeLog = []; } } /***************************************/ diff --git a/aio/content/examples/lifecycle-hooks/src/app/spy.component.ts b/aio/content/examples/lifecycle-hooks/src/app/spy.component.ts index 0f3b0d35f1..74a0c39da1 100644 --- a/aio/content/examples/lifecycle-hooks/src/app/spy.component.ts +++ b/aio/content/examples/lifecycle-hooks/src/app/spy.component.ts @@ -34,7 +34,7 @@ export class SpyParentComponent { } reset() { this.logger.log('-- reset --'); - this.heroes.length = 0; + this.heroes = []; this.logger.tick(); } } diff --git a/aio/content/examples/toh-pt4/src/app/message.service.ts b/aio/content/examples/toh-pt4/src/app/message.service.ts index 9b3a2cac05..1c56a49e70 100644 --- a/aio/content/examples/toh-pt4/src/app/message.service.ts +++ b/aio/content/examples/toh-pt4/src/app/message.service.ts @@ -9,6 +9,6 @@ export class MessageService { } clear() { - this.messages.length = 0; + this.messages = []; } } diff --git a/aio/content/examples/toh-pt5/src/app/message.service.ts b/aio/content/examples/toh-pt5/src/app/message.service.ts index 9b3a2cac05..1c56a49e70 100644 --- a/aio/content/examples/toh-pt5/src/app/message.service.ts +++ b/aio/content/examples/toh-pt5/src/app/message.service.ts @@ -9,6 +9,6 @@ export class MessageService { } clear() { - this.messages.length = 0; + this.messages = []; } } diff --git a/aio/content/examples/toh-pt6/src/app/message.service.ts b/aio/content/examples/toh-pt6/src/app/message.service.ts index 9b3a2cac05..1c56a49e70 100644 --- a/aio/content/examples/toh-pt6/src/app/message.service.ts +++ b/aio/content/examples/toh-pt6/src/app/message.service.ts @@ -9,6 +9,6 @@ export class MessageService { } clear() { - this.messages.length = 0; + this.messages = []; } } diff --git a/aio/content/examples/universal/src/app/message.service.ts b/aio/content/examples/universal/src/app/message.service.ts index 9b3a2cac05..1c56a49e70 100644 --- a/aio/content/examples/universal/src/app/message.service.ts +++ b/aio/content/examples/universal/src/app/message.service.ts @@ -9,6 +9,6 @@ export class MessageService { } clear() { - this.messages.length = 0; + this.messages = []; } }