| 1 | --- |
| 2 | title: Avoid Layout Thrashing |
| 3 | impact: MEDIUM |
| 4 | impactDescription: prevents forced synchronous layouts and reduces performance bottlenecks |
| 5 | tags: javascript, dom, css, performance, reflow, layout-thrashing |
| 6 | --- |
| 7 | |
| 8 | ## Avoid Layout Thrashing |
| 9 | |
| 10 | Avoid interleaving style writes with layout reads. When you read a layout property (like `offsetWidth`, `getBoundingClientRect()`, or |
| 11 | |
| 12 | **This is OK (browser batches style changes):** |
| 13 | |
| 14 | ```typescript |
| 15 | function updateElementStyles(element: |
| 16 | // Each line invalidates style, but browser batches the recalculation |
| 17 | element.style.width |
| 18 | element.style.height |
| 19 | element.style.backgroundColor |
| 20 | element.style.border |
| 21 | } |
| 22 | ``` |
| 23 | |
| 24 | **Incorrect (interleaved reads and writes force reflows):** |
| 25 | |
| 26 | ```typescript |
| 27 | function layoutThrashing(element: |
| 28 | element.style.width |
| 29 | const width = |
| 30 | element.style.height |
| 31 | const height = |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | **Correct (batch writes, then read once):** |
| 36 | |
| 37 | ```typescript |
| 38 | function updateElementStyles(element: |
| 39 | // Batch all writes together |
| 40 | element.style.width |
| 41 | element.style.height |
| 42 | element.style.backgroundColor |
| 43 | element.style.border |
| 44 | |
| 45 | // Read after all writes are done (single reflow) |
| 46 | const { width, |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | **Correct (batch reads, then writes):** |
| 51 | |
| 52 | ```typescript |
| 53 | function avoidThrashing(element: |
| 54 | // Read phase - all layout queries first |
| 55 | const rect1 = |
| 56 | const offsetWidth = |
| 57 | const offsetHeight = |
| 58 | |
| 59 | // Write phase - all style changes after |
| 60 | element.style.width |
| 61 | element.style.height |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | **Better: use CSS classes** |
| 66 | |
| 67 | ```css |
| 68 | .highlighted-box { |
| 69 | width: 100px; |
| 70 | height: 200px; |
| 71 | background-color: blue; |
| 72 | border: 1px solid |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | ```typescript |
| 77 | function updateElementStyles(element: |
| 78 | element.classList.add |
| 79 | |
| 80 | const { width, |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | **React example:** |
| 85 | |
| 86 | ```tsx |
| 87 | // Incorrect: interleaving style changes with layout queries |
| 88 | function Box({ isHighlighted |
| 89 | const ref = |
| 90 | |
| 91 | useEffect(() => |
| 92 | if (ref.current |
| 93 | ref.current.style |
| 94 | const width = |
| 95 | ref.current.style |
| 96 | } |
| 97 | }, [isHighlighted]); |
| 98 | |
| 99 | return <div ref |
| 100 | } |
| 101 | |
| 102 | // Correct: toggle class |
| 103 | function Box({ isHighlighted |
| 104 | return <div className |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | Prefer CSS classes over inline styles when possible. CSS files are cached by the browser, and classes provide better separation of concerns and are easier to maintain. |
| 109 | |
| 110 | See [this gist](https://gist.github.com/paulirish/5d52fb081b3570c81e3a |