Frontend Development 7 min read

Implementing Zero-Delay Timers in JavaScript: Alternatives to setTimeout

This article explores why the native setTimeout cannot achieve a true 0 ms delay, presents a postMessage‑based zero‑timeout implementation, and compares several asynchronous JavaScript techniques—including queueMicrotask, async/await, and MessageChannel—showing their performance advantages over setTimeout.

Fulu Network R&D Team
Fulu Network R&D Team
Fulu Network R&D Team
Implementing Zero-Delay Timers in JavaScript: Alternatives to setTimeout

Inspired by an interview question about achieving a perfectly punctual setTimeout , the author investigated the MDN documentation which states that the minimum delay is >= 4 ms, making a true 0 ms timer impossible with the native API.

To bypass this limitation, a postMessage -based approach called setZeroTimeout is introduced, which schedules callbacks as macro‑tasks. A demo shows that 100 iterations of setZeroTimeout complete in about 15 ms, whereas setTimeout(0) takes roughly 488 ms.

To evaluate other ways of creating a zero‑delay timer, the article defines a baseline setTimeout benchmark that repeatedly schedules a callback 100 times and logs the total execution time.

(function() {
	let i = 0;
	const start = Date.now();
	function test() {
		if (i++ < 100) {
			setTimeout(test);
		} else {
			console.log('setTimeout execution time:', Date.now() - start);
		}
	}
	setTimeout(test);
})();

queueMicrotask

The queueMicrotask API adds a micro‑task that runs before rendering. An implementation of setZeroTimeout using this API is provided, and the benchmark shows execution in about 2 ms compared to 490 ms for setTimeout . The article notes that, although fast, queueMicrotask can block rendering, so APIs like requestAnimationFrame or requestIdleCallback are often preferred.

(function() {
	function setZeroTimeout(fn) { queueMicrotask(fn); }
	let i = 0;
	const start = Date.now();
	function test() {
		if (i++ < 100) {
			setZeroTimeout(test);
		} else {
			console.log('setZeroTimeout execution time:', Date.now() - start);
		}
	}
	setZeroTimeout(test);
})();

async/await

Using async / await , a zero‑delay timer can be built by resolving a promise and chaining callbacks. The benchmark again yields roughly 2 ms versus 490 ms for the native timer.

(function() {
	async function setAsyncTimeout(fn) { Promise.resolve().then(fn); }
	let i = 0;
	const start = Date.now();
	async function test() {
		if (i++ < 100) {
			await setAsyncTimeout(test);
		} else {
			console.log('setAsyncTimeout execution time:', Date.now() - start);
		}
	}
	setAsyncTimeout(test);
})();

MessageChannel

The MessageChannel API creates a new communication channel whose ports can post messages, effectively scheduling a macro‑task. An implementation using this API is shown, and the benchmark records about 4 ms, slightly slower than the micro‑task approaches because macro‑tasks run later in the event loop.

(function() {
	const channel = new MessageChannel();
	function setMessageChannelTimeout(fn) { channel.port2.postMessage(null); }
	channel.port1.onmessage = function() { test(); };
	let i = 0;
	const start = Date.now();
	function test() {
		if (i++ < 100) {
			setMessageChannelTimeout(test);
		} else {
			console.log('setMessageChannelTimeout execution time:', Date.now() - start);
		}
	}
	setMessageChannelTimeout(test);
})();

All three techniques demonstrate that JavaScript provides multiple asynchronous mechanisms to achieve near‑zero delay timers, each with its own trade‑offs regarding task type (micro‑task vs macro‑task) and impact on rendering.

The article invites readers to share additional methods and concludes with references to the original interview question, a blog post on faster timeouts, and a demo page.

PerformanceJavaScriptmicrotasksetTimeoutMessageChannelzero-delay
Fulu Network R&D Team
Written by

Fulu Network R&D Team

Providing technical literature sharing for Fulu Holdings' tech elite, promoting its technologies through experience summaries, technology consolidation, and innovation sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.