Skip to content

Commit

Permalink
fix: avoid recomputing topics in loop (#189)
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
rbergman authored Feb 3, 2021
1 parent 7c11ab4 commit eccb862
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down

0 comments on commit eccb862

Please sign in to comment.