Prevent unnecessary DOM append call when canvas is reused (#17864)

This commit is contained in:
Ferhat 2020-04-21 23:00:02 -07:00 committed by GitHub
parent 18df41928a
commit 8d7071ea46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -130,7 +130,12 @@ class _CanvasPool extends _SaveStackTracking {
if (isFirstChildElement) {
_canvas.style.zIndex = '-1';
}
_rootElement.append(_canvas);
// Before appending canvas, check if canvas is already on rootElement. This
// optimization prevents DOM .append call when a PersistentSurface is
// reused. Reading lastChild is faster than append call.
if (_rootElement.lastChild != _canvas) {
_rootElement.append(_canvas);
}
_context = _canvas.context2D;
_contextHandle = ContextStateHandle(_context);
_initializeViewport(requiresClearRect);