Record Date.now() before setTimeout, then measure the actual delay inside the callback. If the actual delay is much larger than the specified delay, the main thread is being blocked.
setTimeout and Blocking the Main Thread in JavaScript
How do you measure if setTimeout is being delayed by blocking code?
The answer
More FAQs in setTimeout and Blocking the Main Thread in JavaScript
Because setTimeout callbacks go to the macrotask queue. The event loop can only move them to the call stack when it is empty. If a synchronous function is blocking the stack, all queued callbacks wait.
Break long tasks into chunks with setTimeout(cb, 0). Use Web Workers for heavy computation. Use async APIs (fetch, fs.promises). Use requestIdleCallback for low-priority work.
Yes. The timer fires at 1 second, but the callback goes to the macrotask queue. The event loop cannot run it while the blocking function is on the stack. The callback runs after 5 seconds.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.