· Arnau López
How 3D CSS made my component store slow
The StackD store shows every component live on one page, 47 of them. After a busy week of adding new ones, the bottom half of the page started to feel heavy. Scrolling got slower and my laptop got warm.
Measuring it
I opened the store in headless Chrome and measured how busy the page was at different scroll positions. At the top, the main thread was busy about 12 % of the time. From the middle down, 93 to 96 %.
The surprise: JavaScript was only 1 to 2 % of that. The code was fine. The browser was spending its time on painting and compositing, so the problem had to be in the CSS.
Finding the cause
The next thing to count was compositor layers: the pieces of the page the browser draws separately on the GPU. At the top there were about 200. In the middle, about 1,300. When I matched every layer to the component it came from, 977 of them belonged to one component: the Activity grid.
The Activity grid is a grid of days with a 3D view, where every day rises into a bar. To make the 3D work, every cell had transform-style: preserve-3d, a translateZ and four sides rotated in 3D. The catch: all of that was still there in the flat view, when nothing was 3D at all. For the browser, each of those cells is its own layer, and it has to sort them again every time something else on the page moves.
The fix
Use 3D only while the 3D view is on. The grid and every cell stay flat otherwise:
transformStyle: three ? "preserve-3d" : "flat"The bars and their sides are only drawn in 3D. In the flat view, the little lift under the cursor used translateZ(h). With a perspective of 1100 px, that only makes the square a bit bigger, so a plain scale looks the same and stays flat:
transform: h > 0.5 ? `scale(${1100 / (1100 - h)})` : "none"The result
Layers in the middle of the page went from 1,281 to 304. The main thread at the bottom went from 96 % to 32 %, and in the middle from 93 % to 50 %. The flat and 3D views look the same as before.
Try it
This is the Activity grid with the fix. Point at the days, then switch to 3D.
Activity
1,162commits
Last 26 weeks
Active days
142
Busiest day
20
Longest streak
19d
What I check now
If a component has a 3D mode, I look at its layers in the normal mode too. In Chrome DevTools, the Layers panel shows them. 3D CSS that you can't see still costs you.