From eccb862e725f28d305de5cccde109c15abbb5f98 Mon Sep 17 00:00:00 2001 From: Bob Bergman Date: Wed, 3 Feb 2021 12:33:19 -0700 Subject: [PATCH] fix: avoid recomputing topics in loop (#189) Fixes #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 }) }