Why do browsers clamp timeouts and intervals?

I’ve been working on some abstractions of setTimeout and setInterval in order to process large sets of data without blocking the event loop in the browser.

Upon this, I have discovered that browsers “clamp” the number of milliseconds you specify for a timeout or an interval.

For example, if you write setTimeout('do something', 0), the browser will actually do setTimeout('do something', 4). In other words, the browser will force a minimum of 2 to 10 milliseconds, depending on which browser and version your code is running on.

This can have a big impact on the amount of time for a callback/promise to wait. There are ways around this (for timeouts, anyway), such as using postMessage or MessageChannel (poor solutions, in my opinion); they can provide the same effect as a timeout while effectively having 0 wait time.

Why do browsers clamp timeouts and intervals this way? I can find no reasoning for this, official or otherwise. My only hypothesis is that it somehow allows room other tasks to be added to the call stack, but this doesn’t seem to be an issue with postMessage as far as I can tell, so beyond that I can’t think of any reason why this helps anyone.

3

I cannot tell much about the reasons why, but following some “official” documentation that can help on your investigation of the subject.

Above all the HTML 5 Spec mentioned below may contain interesting hints to continue the investigation.

The MDN Documentation on setTimeout says:

Minimum/ maximum delay and timeout nesting

Historically browsers implement setTimeout() “clamping”: successive
setTimeout() calls with delay smaller than the “minimum delay” limit
are forced to use at least the minimum delay. The minimum delay,
DOM_MIN_TIMEOUT_VALUE, is 4 ms (stored in a preference in Firefox:
dom.min_timeout_value), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL of 5ms.

In fact, 4ms is specified by the HTML5 spec and is consistent across
browsers released in 2010 and onward. Prior to (Firefox 5.0 /
Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested
timeouts was 10 ms.

In addition to “clamping”, the timeout can also fire later when the
page (or the OS/browser itself) is busy with other tasks.

To implement a 0 ms timeout in a modern browser, you can use
window.postMessage() as described here.

Browsers including Internet Explorer, Chrome, Safari, and Firefox
store the delay as a 32-bit signed Integer internally. This causes an
Integer overflow when using delays larger than 2147483647, resulting
in the timeout being executed immediately.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why do browsers clamp timeouts and intervals?

I’ve been working on some abstractions of setTimeout and setInterval in order to process large sets of data without blocking the event loop in the browser.

Upon this, I have discovered that browsers “clamp” the number of milliseconds you specify for a timeout or an interval.

For example, if you write setTimeout('do something', 0), the browser will actually do setTimeout('do something', 4). In other words, the browser will force a minimum of 2 to 10 milliseconds, depending on which browser and version your code is running on.

This can have a big impact on the amount of time for a callback/promise to wait. There are ways around this (for timeouts, anyway), such as using postMessage or MessageChannel (poor solutions, in my opinion); they can provide the same effect as a timeout while effectively having 0 wait time.

Why do browsers clamp timeouts and intervals this way? I can find no reasoning for this, official or otherwise. My only hypothesis is that it somehow allows room other tasks to be added to the call stack, but this doesn’t seem to be an issue with postMessage as far as I can tell, so beyond that I can’t think of any reason why this helps anyone.

3

I cannot tell much about the reasons why, but following some “official” documentation that can help on your investigation of the subject.

Above all the HTML 5 Spec mentioned below may contain interesting hints to continue the investigation.

The MDN Documentation on setTimeout says:

Minimum/ maximum delay and timeout nesting

Historically browsers implement setTimeout() “clamping”: successive
setTimeout() calls with delay smaller than the “minimum delay” limit
are forced to use at least the minimum delay. The minimum delay,
DOM_MIN_TIMEOUT_VALUE, is 4 ms (stored in a preference in Firefox:
dom.min_timeout_value), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL of 5ms.

In fact, 4ms is specified by the HTML5 spec and is consistent across
browsers released in 2010 and onward. Prior to (Firefox 5.0 /
Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested
timeouts was 10 ms.

In addition to “clamping”, the timeout can also fire later when the
page (or the OS/browser itself) is busy with other tasks.

To implement a 0 ms timeout in a modern browser, you can use
window.postMessage() as described here.

Browsers including Internet Explorer, Chrome, Safari, and Firefox
store the delay as a 32-bit signed Integer internally. This causes an
Integer overflow when using delays larger than 2147483647, resulting
in the timeout being executed immediately.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật