JavaScript Performance Optimization Techniques Quiz
Answer 35 questions on minimizing reflows, debouncing/throttling, faster loops, efficient structures, avoiding leaks, web workers, lazy loading, caching, cutting re-renders, and profiling with DevTools.
Question 1
Why do repeated style reads and writes in sequence hurt performance?
Question 2
What prints and why?
const el = document.getElementById('box')
const width = el.offsetWidth
el.style.width = width + 10 + 'px'
const height = el.offsetHeight
console.log(width, height)Question 3
How does batching DOM writes help performance?
Question 4
Why is translating elements with CSS transforms often cheaper than changing top/left?
Question 5
What prints?
const log = []
const handle = _.debounce(() => log.push(Date.now()), 200)
handle(); handle(); handle()
setTimeout(() => console.log(log.length), 400)
Question 6
Why use throttling on scroll handlers?
Question 7
What logs?
const throttled = _.throttle(() => console.log('tick'), 100)
let count = 0
const timer = setInterval(() => {
throttled()
if (++count === 5) clearInterval(timer)
}, 20)
Question 8
How can debouncing improve autocomplete UX?
Question 9
What prints?
const arr = new Array(1_000_000).fill(0)
console.time('loop')
for (let i = 0, len = arr.length; i < len; i++) {}
console.timeEnd('loop')Question 10
Why might for-of be slower than traditional for loops in tight hot paths?
Question 11
What prints?
const data = [...Array(5)].map((_, i) => i)
console.log(data.reduce((sum, value) => sum + value, 0))Question 12
How does chunking long loops via requestAnimationFrame help?
Question 13
Why can Maps be faster than plain Objects for frequent inserts/deletes?
Question 14
What prints?
const cache = new Map()
cache.set('a', 1)
cache.set('b', 2)
cache.delete('a')
console.log(cache.size, cache.has('a'))Question 15
When do typed arrays provide optimization benefits?
Question 16
Why are WeakMaps helpful for caching metadata about DOM nodes?
Question 17
Which scenario commonly causes leaks in single-page apps?
Question 18
What prints?
function createListener(node) {
const handler = () => {}
node.addEventListener('click', handler)
return { release() { node.removeEventListener('click', handler) } }
}
const button = document.createElement('button')
const tracker = createListener(button)
tracker.release()
console.log(getEventListeners(button).click?.length || 0)Question 19
How can closures cause leaks in long-lived timers?
Question 20
Why does observing heap snapshots help locate leaks?
Question 21
What prints?
const worker = new Worker('worker.js')
worker.postMessage({ size: 1e7 })
worker.onmessage = (event) => {
console.log('done', event.data.duration)
}
Question 22
Why are workers suited for CPU-heavy parsing?
Question 23
What is the cost of using Web Workers?
Question 24
What prints?
// inside worker.js
self.onmessage = ({ data }) => {
const start = performance.now()
let total = 0
for (let i = 0; i < data.size; i++) total += i
self.postMessage({ duration: performance.now() - start, total })
}Question 25
How does lazy loading images improve performance?
Question 26
When should dynamic import() be combined with IntersectionObserver?
Question 27
Why is prefetching different from lazy loading?
Question 28
What prints?
const memoize = (fn) => {
const cache = new Map()
return (key) => {
if (cache.has(key)) return cache.get(key)
const result = fn(key)
cache.set(key, result)
return result
}
}
const slowDouble = memoize((n) => n * 2)
console.log(slowDouble(5), slowDouble(5))Question 29
Why must cache invalidation be planned?
Question 30
How does request-level caching help APIs?
Question 31
Why is memoizing selectors popular in state management libraries?
Question 32
What prints?
const App = () => {
const [count, setCount] = useState(0)
const memoizedValue = useMemo(() => expensiveCalc(count), [count])
console.log('render', memoizedValue)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
Question 33
How does shouldComponentUpdate or React.memo help performance?
Question 34
Why is splitting state into smaller pieces beneficial?
Question 35
How does the Performance panel in DevTools assist optimization?
