From 1242b9152e1ae9d8f40629d5fd5f15a94bcca014 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 21 Apr 2025 01:12:12 +0200 Subject: [PATCH] LibWeb: Turn Element.scroll(0, 0) into a no-op when possible For unscrolled elements (already at 0, 0) that aren't eligible to be the Document.scrollingElement, we can short-circuit here and avoid doing a synchronous relayout of the page. This avoids a bunch of synchronous layouts on Speedometer3's NewsSite subtests. --- Libraries/LibWeb/DOM/Element.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 7ddcd37b06e..9af9b500461 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -2813,6 +2813,16 @@ void Element::scroll(double x, double y) if (document.document_element() == this && document.in_quirks_mode()) return; + // OPTIMIZATION: Scrolling an unscrolled element to (0, 0) is a no-op as long + // as the element is not eligible to be the Document.scrollingElement. + if (x == 0 + && y == 0 + && scroll_offset(ScrollOffsetFor::Self).is_zero() + && this != document.body() + && this != document.document_element()) { + return; + } + // NOTE: Ensure that layout is up-to-date before looking at metrics. document.update_layout(UpdateLayoutReason::ElementScroll);