# JavaScript Performance Optimization

> What are common JavaScript performance optimization techniques? Explain debouncing and throttling.

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-performance-optimization
Last updated: 2026-11-28

Difficulty: Advanced

## Question

What are common JavaScript performance optimization techniques? Explain debouncing and throttling.

## Answer

Key performance techniques:

Debounce: delays execution until a pause in events. Use for search input, window resize - runs once after the user stops.

Throttle: limits execution to at most once per N ms. Use for scroll events, mousemove - ensures consistent rate.

requestAnimationFrame: schedules visual updates in sync with the browser's repaint cycle (~60fps). Avoids jank.

Web Workers: runs CPU-intensive code on a background thread, keeping the UI thread responsive.

Virtual scrolling: only renders visible rows. Code splitting & lazy loading: load only what is needed.

**Debounce vs throttle on a timeline. Change the strategy, wait and event pattern under Tweak.**

1. **No limiting**: The handler runs on every event.
2. **Debounce**: Each event restarts a timer; the handler runs once after a pause.
3. **Throttle**: At most one call per wait window.
4. **When to use**: Debounce search and resize; throttle scroll and mousemove.

## Code examples

### Debounce and Throttle

```
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

function throttle(fn, interval) {
  let lastTime = 0;
  return function(...args) {
    const now = Date.now();
    if (now - lastTime >= interval) {
      lastTime = now;
      return fn.apply(this, args);
    }
  };
}

const onSearch = debounce((query) => {
  console.log('Searching for:', query);
}, 300);

onSearch('r');
onSearch('re');
onSearch('react');
// After 300ms: Searching for: react

const onScroll = throttle(() => {
  console.log('Scroll handler at:', Date.now());
}, 200);
```

Debounce groups rapid events into one. Throttle limits frequency. Both prevent excessive callback execution.

### requestAnimationFrame and Web Workers

```
function animate(element, targetX) {
  let currentX = 0;
  function step() {
    currentX += (targetX - currentX) * 0.1;
    element.style.transform = `translateX(${currentX}px)`;
    if (Math.abs(targetX - currentX) > 0.5) {
      requestAnimationFrame(step);
    }
  }
  requestAnimationFrame(step);
}

const workerCode = `
  self.onmessage = function(e) {
    const result = e.data.reduce((sum, n) => sum + n, 0);
    self.postMessage(result);
  };
`;

const blob = new Blob([workerCode], { type: 'application/javascript' });
const worker = new Worker(URL.createObjectURL(blob));

worker.onmessage = (e) => {
  console.log('Sum:', e.data);
  worker.terminate();
};

worker.postMessage(Array.from({ length: 100 }, (_, i) => i + 1));

performance.mark('start');
performance.mark('end');
performance.measure('op', 'start', 'end');
console.log(performance.getEntriesByName('op')[0].duration);
```

rAF batches DOM updates with the browser paint cycle. Web Workers move CPU work off the main thread.

## Key points

- Debounce is for 'wait until quiet', throttle is for 'run at most N times per second'
- Avoid layout thrashing: reading layout then writing in a loop causes forced reflows
- Web Workers have no DOM access - communicate via postMessage
- Use will-change: transform CSS for GPU-accelerated animations

## Common follow-up questions

- What is layout thrashing and how do you avoid it?
- When would you use requestAnimationFrame instead of CSS animations?
- How do Web Workers communicate with the main thread?

## Interview tips

- Implement debounce and throttle from scratch - very common coding interview question
- Explain the difference clearly: debounce = delay, throttle = rate limit
- Mention rAF for animations and Web Workers for CPU tasks - shows depth

## Concepts covered

Debounce, Throttle, requestAnimationFrame, Web Workers

Previous: [Advanced Regular Expressions](https://www.internhack.in/learn/interview/javascript-interview/js-regex-advanced) · Next: [Tagged Template Literals](https://www.internhack.in/learn/interview/javascript-interview/js-tagged-templates) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Intersection Observer and Mutation Observer](https://www.internhack.in/learn/interview/javascript-interview/js-intersection-observer)
- [Async Iterators and for await...of](https://www.internhack.in/learn/interview/javascript-interview/js-async-iterators)
- [Advanced Object Patterns and Mixins](https://www.internhack.in/learn/interview/javascript-interview/js-object-patterns)
- [ES Modules vs CommonJS](https://www.internhack.in/learn/interview/javascript-interview/js-modules-advanced)
- [Promise Combinators](https://www.internhack.in/learn/interview/javascript-interview/js-promise-combinators)
- [Variables: var, let, const](https://www.internhack.in/learn/interview/javascript-interview/js-var-let-const)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
