Advertisement
Both browser and NodeJS javascript environments throttles with a minimum delay that is greater than 0ms. That means even though setting a delay of 0ms will not happen instantaneously.
Browsers: They have a minimum delay of 4ms. This throttle occurs when successive calls are triggered due to callback nesting(certain depth) or after a certain number of successive intervals.
Note: The older browsers have a minimum delay of 10ms.
Nodejs: They have a minimum delay of 1ms. This throttle happens when the delay is larger than 2147483647 or less than 1.
The best example to explain this timeout throttling behavior is the order of below code snippet.
1function runMeFirst() {
2
3console.log("My script is initialized");
4}
5
6setTimeout(runMeFirst, 0);
7
8console.log("Script loaded");and the output would be in
1Script loaded
2
3My script is initializedIf you don't use setTimeout, the order of logs will be sequential.
1function runMeFirst() {
2
3console.log("My script is initialized");
4}
5
6runMeFirst();
7
8console.log("Script loaded");and the output is,
1My script is initialized
2
3Script loadedAdvertisement
JavaScript Coding Exercise 46
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement