Render performance optimization
This commit is contained in:
parent
14dd42bad0
commit
4d3df145de
|
@ -1,7 +1,5 @@
|
||||||
import React, { useState, useRef, useEffect } from 'react';
|
import React, { useState, useRef, useEffect } from 'react';
|
||||||
import setPosition from './setPosition';
|
import setPosition from './setPosition';
|
||||||
import TagWidget from './widgets/tag/TagWidget';
|
|
||||||
import CommentWidget from './widgets/comment/CommentWidget';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The popup editor component.
|
* The popup editor component.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import WebAnnotation from '../WebAnnotation';
|
|
||||||
|
|
||||||
const TEXT = 3; // HTML DOM node type for text nodes
|
const TEXT = 3; // HTML DOM node type for text nodes
|
||||||
|
|
||||||
|
const RENDER_BATCH_SIZE = 100; // Number of annotations to render in one frame
|
||||||
|
|
||||||
const uniqueItems = items => Array.from(new Set(items))
|
const uniqueItems = items => Array.from(new Set(items))
|
||||||
|
|
||||||
export default class Highlighter {
|
export default class Highlighter {
|
||||||
|
@ -12,13 +12,30 @@ export default class Highlighter {
|
||||||
}
|
}
|
||||||
|
|
||||||
init = annotations => {
|
init = annotations => {
|
||||||
|
const startTime = performance.now();
|
||||||
|
|
||||||
// Discard all annotations without a TextPositionSelector
|
// Discard all annotations without a TextPositionSelector
|
||||||
const highlights = annotations.filter(a => a.selector('TextPositionSelector'));
|
const highlights = annotations.filter(a => a.selector('TextPositionSelector'));
|
||||||
|
|
||||||
// Sorting bottom to top significantly speeds things up,
|
// Sorting bottom to top significantly speeds things up,
|
||||||
// because walkTextNodes will have a lot less to walk
|
// because walkTextNodes will have a lot less to walk
|
||||||
highlights.sort((a, b) => b.start - a.start);
|
highlights.sort((a, b) => b.start - a.start);
|
||||||
highlights.forEach(this._addAnnotation);
|
|
||||||
|
// Render loop
|
||||||
|
const render = annotations => {
|
||||||
|
const batch = annotations.slice(0, RENDER_BATCH_SIZE);
|
||||||
|
const remainder = annotations.slice(RENDER_BATCH_SIZE);
|
||||||
|
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
batch.forEach(this._addAnnotation);
|
||||||
|
if (remainder.length > 0)
|
||||||
|
render(remainder);
|
||||||
|
else
|
||||||
|
console.log(`Rendered ${highlights.length}, took ${performance.now() - startTime}ms`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
render(highlights);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addAnnotation = annotation => {
|
_addAnnotation = annotation => {
|
||||||
|
|
Loading…
Reference in New Issue