That's an excellent question, and you've hit upon a subtle but important point about how browsers handle DOM manipulations!
You are absolutely correct: toggling a class with JavaScript (document.body.classList.toggle('at-bottom', atBottom);) is still a DOM write operation. It modifies the class attribute of the element.
The reason this approach is often recommended for avoiding layout thrashing, even though it's a write, is due to batching and browser optimization:
Batching of Writes: Browsers are very clever about how they handle DOM writes. When you make multiple style changes or class changes in quick succession (especially within the same event loop or script execution), the browser will often batch these operations. Instead of immediately recalculating layout and repainting for each individual change, it waits until it absolutely has to.
The browser might consolidate these writes and perform only one layout recalculation and repaint at the end of the script, or when it needs to render something that requires up-to-date layout information (like reading a layout property).
Offloading to CSS Engine: When you use CSS classes, you are essentially telling the browser's CSS engine: "When this element has this class, apply these styles." The browser's CSS engine is highly optimized to manage these declarative style rules. Applying a class change can trigger a cascade of style updates, but again, the engine tries to do this efficiently, often deferring the actual style application and layout recalculation until it's necessary.
Separation of Concerns & Read/Write Separation: The core principle of avoiding thrashing is separating reads from writes. You perform all your DOM reads first, then all your DOM writes second.
Writes:atBottom = newAtBottom;, if (lock) { lock.style.visibility = ... }, lightenThread(); (assuming lightenThread doesn't perform reads that trigger reflows).
The suggestion was to group the writes:
javascript
// Batch all reads first
const previous = atBottom;
const newAtBottom = isAtBottom();
const lock = document.getElementById("lock"); // This is a read, but often considered lightweight
// Then batch all writes
atBottom = newAtBottom; // This is a write to a variable, not DOM
if (lock) {
lock.style.visibility = atBottom ? "visible" : "hidden"; // DOM Write 1
}
if (!previous && atBottom) {
lightenThread(); // Potentially DOM Write(s)
}
The CSS alternative for lock.style.visibility = ... is just replacing DOM Write 1 with document.body.classList.toggle('at-bottom', atBottom);. This single DOM write operation (toggling the class) is much more likely to be batched efficiently by the browser's rendering engine, especially if it's the only class change or style manipulation being done after all the reads.
In summary:
While toggling a class is a write, it's a write that the browser's rendering pipeline is designed to handle very efficiently, often by batching it with other style updates and deferring the actual layout recalculation until a more opportune moment. This makes it generally safer and more performant than interleaving multiple small reads and writes, or directly manipulating inline styles repeatedly. The key benefit is still the strict separation of reads and writes.