Skip to content
FrontDepth, Home

Back to the topic

Practice: Reflow, repaint and layout thrashing

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 3 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    The script runs on a blank page. What does it log?

    JavaScript
    const list = document.createElement("div");
    document.body.appendChild(list);
    for (let i = 0; i < 3; i++) {
      const row = document.createElement("div");
      row.style.height = "20px";
      list.appendChild(row);
    }
    
    const cached = list.offsetHeight;
    list.firstElementChild.style.height = "50px";
    console.log(cached);
    console.log(list.offsetHeight);
    
    list.lastElementChild.remove();
    console.log(list.offsetHeight);
    
    list.style.height = "40px";
    console.log(list.offsetHeight);
    console.log(list.scrollHeight);
  2. Open questionJunior

    What is the difference between reflow and repaint, and which kinds of CSS changes cause each?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    The script runs on a blank page. In what order do the lines appear, and with which numbers?

    JavaScript
    const box = document.createElement("div");
    box.style.width = "100px";
    document.body.appendChild(box);
    
    new ResizeObserver((entries) => {
      console.log("resize " + entries[0].contentRect.width);
    }).observe(box);
    
    requestAnimationFrame(() => {
      console.log("raf " + box.offsetWidth);
      box.style.width = "150px";
    });
    
    Promise.resolve().then(() => console.log("microtask " + box.offsetWidth));
    console.log("sync " + box.offsetWidth);
  4. Open questionMiddle

    A dashboard redraws a 30-bar chart on every filter change, and the click feels slow. The Performance panel shows dozens of narrow Layout events inside one task. What is happening, and how do you fix it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    The script runs in Chromium on a blank page. What does it log?

    JavaScript
    const section = document.createElement("section");
    const content = document.createElement("div");
    content.style.height = "500px";
    section.appendChild(content);
    document.body.appendChild(section);
    console.log(section.offsetHeight);
    
    section.style.contentVisibility = "hidden";
    console.log(section.offsetHeight);
    
    section.style.containIntrinsicHeight = "200px";
    console.log(section.offsetHeight);
    console.log(content.offsetHeight);
    
    section.style.contentVisibility = "visible";
    console.log(section.offsetHeight);
  6. Open questionSenior

    A side drawer opens by animating its width from 0 to 320px and stutters on mid-range phones. How would you make it smooth, and what does your fix cost?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.