From 4cf9cbc9eb8c5371b4fed41c222903b708db2692 Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Sat, 28 Oct 2023 18:13:16 +0200 Subject: [PATCH] performance issue --- CHANGELOG.md | 1 + plugins/reading_info.ts | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4b57eed..076486c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case ## [Unreleased] ### Fixed - Wrong urls in `pagefind` with `base_path` plugin [#502]. +- Performance issue in `reading_info` plugin. - Updated deps: `tailwindcss`, `deno_dom`, `highlight.js`, `sass`. ## [1.19.2] - 2023-10-21 diff --git a/plugins/reading_info.ts b/plugins/reading_info.ts index 808163ee..d152958a 100644 --- a/plugins/reading_info.ts +++ b/plugins/reading_info.ts @@ -47,10 +47,15 @@ export default function (userOptions?: Partial) { const segmenter = new Intl.Segmenter(lang, { granularity: "word", }); - const words = segmenter.segment(content); - const wordCount = [...words].filter((word) => word.isWordLike).length; - const minutes = wordCount / options.wordsPerMinute; + let wordCount = 0; + for (const word of segmenter.segment(content)) { + if (word.isWordLike) { + wordCount++; + } + } + + const minutes = wordCount / options.wordsPerMinute; const time = Math.round(minutes * 60 * 1000); const displayTime = Math.ceil(parseFloat(minutes.toFixed(2)));