Skip to content

Commit b10acf8

Browse files
committed
perf: Eliminate Path scrollTop Layout Thrashing in getTargetData (#9403)
1 parent 0f2b97a commit b10acf8

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

src/main/DomEvents.mjs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ class DomEvents extends Base {
284284
}
285285

286286
const result = {
287-
path : path.map(e => this.getTargetData(e, event.type)),
288-
target : this.getTargetData(event.target, event.type),
287+
path : path.map(e => this.getTargetData(e, event.type, false)),
288+
target : this.getTargetData(event.target, event.type, true),
289289
timeStamp: event.timeStamp,
290290
type : event.type,
291291
};
292292

293293
if (event.relatedTarget) {
294-
result.relatedTarget = this.getTargetData(event.relatedTarget, event.type)
294+
result.relatedTarget = this.getTargetData(event.relatedTarget, event.type, false)
295295
}
296296

297297
return result
@@ -373,31 +373,32 @@ class DomEvents extends Base {
373373
path = this.getSelectionPath(path, target.parentNode, eventName);
374374
}
375375

376-
path.push(this.getTargetData(target, eventName));
376+
path.push(this.getTargetData(target, eventName, false));
377377

378378
return path
379379
}
380380

381381
/**
382382
* @param {HTMLElement} node
383383
* @param {String} [eventName]
384+
* @param {Boolean} [isTarget=true]
384385
* @returns {Object}
385386
*/
386-
getTargetData(node, eventName) {
387+
getTargetData(node, eventName, isTarget=true) {
387388
// Fast path: eliminate layout thrashing on continuous events.
388389
// We skip bounding rect and layout dimensions for scroll & wheel.
389390
let skipLayout = eventName === 'scroll' || eventName === 'wheel',
390391
r = !skipLayout && node.getBoundingClientRect?.(),
391-
rect = r && this.parseDomRect(r) || {};
392+
rect = r && this.parseDomRect(r) || undefined;
392393

393394
return {
394395
aria : this.geAriaAttributes(node),
395396
checked : node.checked,
396397
childElementCount: node.childElementCount,
397-
clientHeight : skipLayout ? 0 : node.clientHeight,
398+
clientHeight : skipLayout ? undefined : node.clientHeight,
398399
clientLeft : node.clientLeft,
399400
clientTop : node.clientTop,
400-
clientWidth : skipLayout ? 0 : node.clientWidth,
401+
clientWidth : skipLayout ? undefined : node.clientWidth,
401402
cls : node.classList ? [...node.classList] : [],
402403
data : {...node.dataset},
403404
draggable : node.draggable,
@@ -407,16 +408,16 @@ class DomEvents extends Base {
407408
isConnected : node.isConnected,
408409
isContentEditable: node.isContentEditable,
409410
nodeType : node.nodeType,
410-
offsetHeight : skipLayout ? 0 : node.offsetHeight,
411+
offsetHeight : skipLayout ? undefined : node.offsetHeight,
411412
offsetLeft : node.offsetLeft,
412413
offsetTop : node.offsetTop,
413-
offsetWidth : skipLayout ? 0 : node.offsetWidth,
414+
offsetWidth : skipLayout ? undefined : node.offsetWidth,
414415
rect,
415416
role : node.role,
416-
scrollHeight : skipLayout ? 0 : node.scrollHeight,
417-
scrollLeft : node.scrollLeft,
418-
scrollTop : node.scrollTop,
419-
scrollWidth : skipLayout ? 0 : node.scrollWidth,
417+
scrollHeight : skipLayout ? undefined : node.scrollHeight,
418+
scrollLeft : skipLayout && !isTarget ? undefined : node.scrollLeft,
419+
scrollTop : skipLayout && !isTarget ? undefined : node.scrollTop,
420+
scrollWidth : skipLayout ? undefined : node.scrollWidth,
420421
style : node.style?.cssText,
421422
tabIndex : node.getAttribute?.('tabindex') ? node.tabIndex : null,
422423
tagName : node.tagName?.toLowerCase()

0 commit comments

Comments
 (0)