Skip to content

Commit

Permalink
Change 'lookUpwardForInlineStyle' from O(n^2) to O(n).
Browse files Browse the repository at this point in the history
Summary:
**Summary**

The function 'lookUpwardForInlineStyle' repeatedly called 'getPreviousBlock' which has the complexity O(n). This tanks performance when creating a document with lots of empty lines. Simply holding down the enter key will quickly cause the frame rate to drop below 1fps.

The fix is straightforward and, in my opinion, preferable to a while loop.

**Test Plan**

I did some manual testing. The code was able to correctly (and efficiently) find the correct style.
Closes facebookarchive/draft-js#1429

Differential Revision: D6060417

fbshipit-source-id: 8a14af25f776ac5263747e2fc4bbb665884ad970
  • Loading branch information
aforismesen authored and facebook-github-bot committed Oct 14, 2017
1 parent 788ecb9 commit db24418
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/model/immutable/EditorState.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,17 +652,15 @@ function lookUpwardForInlineStyle(
content: ContentState,
fromKey: string,
): DraftInlineStyle {
var previousBlock = content.getBlockBefore(fromKey);
var previousLength;

while (previousBlock) {
previousLength = previousBlock.getLength();
if (previousLength) {
return previousBlock.getInlineStyleAt(previousLength - 1);
}
previousBlock = content.getBlockBefore(previousBlock.getKey());
}

var lastNonEmpty = content.getBlockMap()
.reverse()
.skipUntil((_, k) => k === fromKey)
.skip(1)
.skipUntil((block, _) => block.getLength())
.first();

if (lastNonEmpty)
return lastNonEmpty.getInlineStyleAt(lastNonEmpty.getLength() - 1);
return OrderedSet();
}

Expand Down

0 comments on commit db24418

Please sign in to comment.