From 14dd42bad05fcbb1b88b71212a1fd08dc7dd168f Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Sun, 5 Apr 2020 18:02:18 +0200 Subject: [PATCH] Performance tweak --- src/highlighter/Highlighter.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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 => {