The curtains were fine. The avatar was not.
Tracking down the jank in a deliberately simple portfolio
I’ve rebuilt my portfolio more times than I’d like to admit, and for a long time I never published any of the versions. Some of that was perfectionism. Some of it was not knowing what I actually wanted to show.
This version is deliberately simple. The idea was to give myself fewer decisions: a narrow set of creative constraints and as little technical overhead as I could get away with. It’s a single page where the content is the focus and the containers flow without many rules. What I want it to say is what I like, what I’m curious about, and hopefully what I know about UX design.
The rule about keeping things technically simple was never going to last. I wanted a little flair, so I animated a pair of curtains that close over the top half of the page. That part went fine, because I have a reasonable sense of how to animate things in a browser and what to avoid. Then I wondered what would happen if I added a simple 3D avatar. Unsurprisingly, that’s where the jank started.
What 3D in a browser actually costs
Getting a 3D model onto a web page is easy now. Libraries like Three.js can have a model on screen in an afternoon. What’s easy to miss is how differently that model behaves from everything else on the page.
A normal page gets painted and then mostly sits still. A WebGL canvas, by default, redraws itself every frame, sixty or more times a second, whether anything has changed or not. Before that first frame there’s a pile of setup work: downloading the library, decoding the model, uploading textures to the GPU, and compiling shaders.
So when the page started stuttering, I assumed the 3D was the problem. It mostly wasn’t.
Finding the jank
To separate the effects, I recorded the page in Chrome DevTools’ Performance panel three ways: curtains only, avatar only, and both together. I also throttled the CPU so my machine would behave more like an average laptop.
On their own, the curtains held up. They only animate transforms, which the browser can handle without much work on the main thread. The most likely culprit wasn’t the rendering either. It was the walker, the script that decides where the avatar goes on the page.
On every frame, the walker asked the browser where elements were and then moved things based on the answer. Reading layout right after changing styles forces the browser to recalculate the layout on the spot, before it would normally get to it. Meanwhile the curtain script was running its own animation loop with its own reads and writes. With the two interleaving, the browser could end up redoing layout several times in a single frame. That’s layout thrashing, and it’s a classic source of jank.
The fixes
If the walker is the culprit, the fixes are fairly standard:
- Cache element positions. Measure each element once relative to the document, then work out its current position from the scroll offset. Only re-measure when something actually resizes, using a ResizeObserver.
- One read phase, one write phase. Do all the DOM reads from both scripts first, then all the writes. Ideally, run both from a single shared animation loop instead of two independent ones.
- Decide less often, move just as smoothly. The walker doesn’t need to rethink its route sixty times a second. Updating its decisions 20 to 30 times a second and interpolating the avatar’s movement in between keeps the motion smooth while the render loop stays at 60fps.
Where I’ve landed, for now
I’m going to come back to this later. The product is simple, but this kind of scene is an edge case. It’s more technically complex than anything that would come up in client work, and putting more time into it now would cut against the reason I started this version: fewer decisions, and content first.
Like the rest of this site, it’s a work in progress. At least this time it’s published.
