perf: avoid measureText() where possible (#1375)

This commit is contained in:
Nolan Lawson 2019-08-07 09:11:15 -07:00 committed by GitHub
parent 774210f776
commit 98e02cf650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)
),