𝟳 𝗛𝗶𝗱𝗱𝗲𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗕𝗼𝘁𝘁𝗹𝗲𝗻𝗲𝗰𝗸𝘀

Slow web apps rarely fail because of bad algorithms. They fail because of how your code talks to the browser.

I profiled over 300 production apps. I found that 73% of performance issues come from these 7 sources.

  1. Layout thrashing This happens when you read a layout property and then write to the DOM immediately after. This forces the browser to recalculate layout multiple times. • Impact: 40-60% slower rendering. • Fix: Batch all your reads first. Then batch all your writes using requestAnimationFrame.

  2. Unbounded event listeners Adding a listener without removing it causes memory leaks. This is a major problem in single-page apps. • Impact: 15-30% memory growth per hour. • Fix: Use an AbortController to clean up listeners when a component unmounts.

  3. Synchronous DOM reads in loops Reading properties like offsetWidth inside a loop that also writes to the DOM causes constant reflows. • Impact: 20-40% frame drops. • Fix: Cache your layout values in a variable before the loop starts.

  4. Missing requestAnimationFrame batching Direct DOM changes on scroll or resize events fire too often. • Impact: 10-25% jank during scrolling. • Fix: Wrap your scroll handlers in requestAnimationFrame to sync with the paint cycle.

  5. Large JSON.parse calls Parsing huge JSON files blocks the main thread. This causes input lag. • Impact: 50-200ms freezes per call. • Fix: Use Web Workers to parse data off the main thread.

  6. Complex CSS selector matching Deeply nested or complex selectors slow down style recalculations. • Impact: 5-15% extra style time. • Fix: Simplify your CSS structure and use flatter selectors.

  7. Duplicate bundle chunks Unoptimized bundles waste bandwidth. • Impact: 100-500KB wasted transfer. • Fix: Use tools like webpack-bundle-analyzer to find and remove duplicate code.

How to measure your progress: • Open Chrome DevTools and go to the Performance tab. • Record a 5-second session. • Look for long tasks in the Main flame chart. • Apply one fix and compare the Rendering and Painting times.

Focus on these areas to improve your Core Web Vitals scores.

Source: https://dev.to/kui_luo/how-to-find-and-fix-7-hidden-performance-bottlenecks-in-your-javascript-code-ek5