From 98e02cf650da9e6ab75ff4f5e6845af2059c042c Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Wed, 7 Aug 2019 09:11:15 -0700 Subject: [PATCH] perf: avoid measureText() where possible (#1375) --- src/routes/_components/status/Status.html | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/routes/_components/status/Status.html b/src/routes/_components/status/Status.html index c3dcfa2b..d4d5f34e 100644 --- a/src/routes/_components/status/Status.html +++ b/src/routes/_components/status/Status.html @@ -232,8 +232,14 @@ visibility: ({ originalStatus }) => originalStatus.visibility, mentions: ({ originalStatus }) => originalStatus.mentions || [], plainTextContent: ({ content, mentions }) => statusHtmlToPlainText(content, mentions), - plainTextContentLength: ({ plainTextContent }) => measureText(plainTextContent), - plainTextContentOverLength: ({ plainTextContentLength }) => plainTextContentLength > LONG_POST_LENGTH, + plainTextContentOverLength: ({ plainTextContent }) => ( + // measureText() is expensive, so avoid doing it when possible. + // Also measureText() typically only makes text shorter, not longer, so we can measure the raw length + // as a shortcut. (The only case where it makes text longer is with short URLs which get expanded to a longer + // placeholder.) This isn't 100% accurate, but we don't need perfect accuracy here because this is just + // to show a "long post" content warning. + plainTextContent.length > LONG_POST_LENGTH && measureText(plainTextContent) > LONG_POST_LENGTH + ), spoilerText: ({ originalStatus, plainTextContentOverLength }) => ( originalStatus.spoiler_text || (plainTextContentOverLength && LONG_POST_TEXT) ),