mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Set --header-offset property only when changed (#15107)
Calling `setProperty("--header-offset", newValue)` will always cause a 'Recalculate Style' event, even if the value is unchanged. On my browser, these 'Recalculate Style' events take about 6-7ms each time the `dockCheck` function is run.
This commit stores the 'previous' value in an instance variable, and only calls setProperty if the value has changed. This brings the total runtime of `dockCheck` down to about 70µs on my machine.
This commit is contained in:
@@ -186,7 +186,13 @@ const SiteHeaderComponent = MountWidget.extend(
|
||||
const headerRect = header.getBoundingClientRect(),
|
||||
headerOffset = headerRect.top + headerRect.height,
|
||||
doc = document.documentElement;
|
||||
doc.style.setProperty("--header-offset", `${headerOffset}px`);
|
||||
|
||||
const newValue = `${headerOffset}px`;
|
||||
if (newValue !== this.currentHeaderOffsetValue) {
|
||||
this.currentHeaderOffsetValue = newValue;
|
||||
doc.style.setProperty("--header-offset", newValue);
|
||||
}
|
||||
|
||||
if (offset >= this.docAt) {
|
||||
if (!this.dockedHeader) {
|
||||
document.body.classList.add("docked");
|
||||
|
||||
Reference in New Issue
Block a user