Skip to content

Commit

Permalink
fix: improved textcell performance for novels (#8986)
Browse files Browse the repository at this point in the history
This fixes an issue where the glide table performance would chug when
given a text cell with lots of text to truncate. while `measureText` is
more performant than getBoundingClientRectangle, it can still be
problematic to call excessively. To remedy, we implement a binary search
for the longest string that's still below max width, reducing the number
of times we need to measure the text for longer texts at the cost of
calling it a few more times for shorter texts.
  • Loading branch information
ashtonG authored Mar 13, 2024
1 parent 89d4708 commit 52572d4
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,18 @@ function truncate(
if (textWidth <= maxWidth || textWidth <= ellipsisWidth) {
return text;
} else {
while (newText.length > 0 && textWidth + ellipsisWidth > maxWidth) {
newText = newText.substring(0, newText.length - 1);
// Binary search for the longest string below max width
let leftBound = 0;
let rightBound = text.length;
while (leftBound < rightBound) {
const subLength = Math.floor((leftBound + rightBound) / 2);
newText = text.substring(0, subLength);
textWidth = ctx.measureText(newText).width;
if (textWidth + ellipsisWidth < maxWidth) {
leftBound = subLength + 1;
} else {
rightBound = subLength;
}
}
return newText + suffix;
}
Expand Down

0 comments on commit 52572d4

Please sign in to comment.