diff --git a/src/highlighter/Highlighter.js b/src/highlighter/Highlighter.js index faee30b..1a31d1a 100644 --- a/src/highlighter/Highlighter.js +++ b/src/highlighter/Highlighter.js @@ -11,13 +11,14 @@ export default class Highlighter { this.formatter = formatter; } - init = annotations => { - // TODO - there are several performance optimzations that are not yet ported - // across from Recogito - annotations - // Discard all annotations without a TextPositionSelector - .filter(annotation => annotation.selector('TextPositionSelector')) - .forEach(annotation => this._addAnnotation(annotation)); + init = annotations => { + // Discard all annotations without a TextPositionSelector + const highlights = annotations.filter(a => a.selector('TextPositionSelector')); + + // Sorting bottom to top significantly speeds things up, + // because walkTextNodes will have a lot less to walk + highlights.sort((a, b) => b.start - a.start); + highlights.forEach(this._addAnnotation); } _addAnnotation = annotation => {