Facebook Pixel

How do you measure if setTimeout is being delayed by blocking code?

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.

Verify This Answer

Cross-check this information using these trusted sources:

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?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.