Skip to content

Commit 9367e29

Browse files
perf(CALEXT2_Slot): measure container rect once before event loop
When hideOverflow is enabled, Event.draw() was calling getBoundingClientRect() on the container inside the loop, causing a redundant forced reflow for every event appended. Move the hideOverflow CSS setup and the container measurement into Slot.drawEvents() so they run once per slot. Pass the pre-measured contentRect to Event.draw() as a parameter.
1 parent dae25fd commit 9367e29

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

CALEXT2_Event.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,11 @@ class Event {
2727
}
2828
}
2929

30-
draw (slot, targetDom) {
31-
const {hideOverflow} = slot;
30+
draw (slot, targetDom, contentRect = null) {
3231
const eventDom = this.dom;
3332
eventDom.style.opacity = 0;
3433
targetDom.appendChild(eventDom);
35-
if (hideOverflow) {
36-
targetDom.classList.add("hideOverflow");
37-
targetDom.style.maxHeight = slot.maxHeight;
38-
targetDom.style.height = slot.maxHeight;
39-
const contentRect = targetDom.getBoundingClientRect();
34+
if (contentRect) {
4035
const eventRect = eventDom.getBoundingClientRect();
4136
if (
4237
eventRect.bottom > contentRect.bottom ||

CALEXT2_Slot.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,30 @@ class Slot {
5252

5353
drawEvents () {
5454
let hiddenCount = 0;
55+
// Apply overflow constraints and read the container rect once before the
56+
// loop. Each appendChild forces a layout recalculation, so measuring
57+
// targetDom inside the loop would cause a redundant reflow per event.
58+
// The container dimensions don't change between appends, so a single
59+
// setup + read up-front is sufficient.
60+
let contentRect = null;
61+
if (this.hideOverflow) {
62+
this.contentDom.classList.add("hideOverflow");
63+
this.contentDom.style.maxHeight = this.maxHeight;
64+
this.contentDom.style.height = this.maxHeight;
65+
contentRect = this.contentDom.getBoundingClientRect();
66+
}
5567
for (let i = 0; i < this.events.length; i++) {
5668
const event = new Event(this.events[i], this);
57-
hiddenCount += this.drawEvent(event);
69+
hiddenCount += this.drawEvent(event, contentRect);
5870
}
5971
if (hiddenCount > 0) {
6072
this.footerDom.querySelector(".hiddenCount").innerHTML =
6173
`+ ${hiddenCount}`;
6274
}
6375
}
6476

65-
drawEvent (event) {
66-
return event.draw(this, this.contentDom);
77+
drawEvent (event, contentRect) {
78+
return event.draw(this, this.contentDom, contentRect);
6779
}
6880

6981
assignEvents (events) {

0 commit comments

Comments
 (0)