From 8442d370f4f21b813305da473dca908217436977 Mon Sep 17 00:00:00 2001 From: Bob Bergman Date: Thu, 21 Jan 2021 13:08:18 -0700 Subject: [PATCH] perf(topics): avoid recomputing topics in loop Aims to fix or improve #101 --- src/index.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index d42db64d..154b8374 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,9 +67,12 @@ export default class Help extends HelpBase { * and this can be removed. */ private get _topics(): Config.Topic[] { - return this.config.topics.filter((topic: Config.Topic) => { + // since this.config.topics is a getter that does non-trivial work, cache it outside the filter loop for + // performance benefits in the presence of large numbers of topics + const topics = this.config.topics + return topics.filter((topic: Config.Topic) => { // it is assumed a topic has a child if it has children - const hasChild = this.config.topics.some(subTopic => subTopic.name.includes(`${topic.name}:`)) + const hasChild = topics.some(subTopic => subTopic.name.includes(`${topic.name}:`)) return hasChild }) }