JavaScript
/Advanced
Browser Rendering Cycle
Definition
The steps the browser takes to convert HTML, CSS, and JS into pixels on the screen (Parse -> Style -> Layout -> Paint -> Composite). The Event Loop coordinates with the rendering engine.
Explain Like I'm New
Painting the screen is expensive. The browser tries to paint the screen 60 times a second (every 16.6ms). However, the browser uses the EXACT SAME THREAD for rendering as it uses for JavaScript. If your JavaScript takes 50ms to run, the browser misses 3 frames, resulting in a visually 'janky' or lagging animation.
Real World Example
Using `requestAnimationFrame` to run a piece of JavaScript right before the browser calculates the next frame, ensuring silky smooth 60FPS animations.
Common Use Cases
- •60FPS Animations
- •Scroll performance optimization
Terminal Output
bash / terminal
// Changing styles multiple times synchronously:
div.style.width = '100px';
div.style.height = '100px';
div.style.backgroundColor = 'red';
// The user NEVER sees these intermediate states.
// Because JavaScript is synchronous here, the Event Loop cannot reach the Render Phase.
// Only the final state (Red 100x100) is painted to the screen in a single batch.
Interview Questions
basic
- What causes 'Jank' on a webpage?
intermediate
- When does the Event Loop decide to render?
advanced
- What is `requestAnimationFrame`?