JS Coding Questions Logo
JS Coding Questions
#388💼 Interview💻 Code

What is minimum timeout throttling

Advertisement

728x90

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.

javascript
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

cmd
1Script loaded
2
3My script is initialized

If you don't use setTimeout, the order of logs will be sequential.

javascript
1function runMeFirst() {
2
3console.log("My script is initialized");
4}
5
6runMeFirst();
7
8console.log("Script loaded");

and the output is,

cmd
1My script is initialized
2
3Script loaded

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 46

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
388of476