Bug 221124 - Not clamp setTimeout(..., 0) to 1ms
Summary: Not clamp setTimeout(..., 0) to 1ms
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-01-29 00:23 PST by Wanming Lin
Modified: 2021-02-04 17:50 PST (History)
11 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Wanming Lin 2021-01-29 00:23:43 PST
Calls to setTimeout(..., 0) is clamped to a 1 ms timeout in WebKit(https://github.com/WebKit/WebKit/blob/main/Source/WebCore/page/DOMTimer.cpp#L384), this is actually a bug since there's no 1ms clamp in the spec(https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps), and which will cause scheduling error, e.g.

setTimeout(()=> console.log('1ms timeout'), 1);
setTimeout(()=> console.log('0ms timeout'), 0);

Outputs:
Safari:  1ms timeout, 0ms timeout

Moreover, 1ms clamp may bring performance penalty.

Compatibility:
Firefox: no 1ms clamp
Safari: 1ms clamp
Chrome: 1ms clamp

Chrome is planning to remove this 1ms clamp, see discussion at https://groups.google.com/a/chromium.org/g/blink-dev/c/HKPTp7C1LwY/m/kIbTxKSPAwAJ
Comment 1 Alexey Proskuryakov 2021-01-31 12:42:08 PST
What is the motivation for changing the behavior, as opposed to correcting the spec? Seems like only bad things can possibly happen with regards to battery life; and there are other reasons why timers would be clamped anyway (nesters timers, backgrounds tabs).

If this is desired to correct ordering, there are other ways to do it, but also unsure if it's a particularly likely case to run into.
Comment 2 Wanming Lin 2021-02-01 05:32:40 PST
Thanks Alexey! One more motivation of this change is performance impact, we can see about 1% beneficial to Speedometer 2 on MiniBrowser on M1 with this change.
Comment 3 Radar WebKit Bug Importer 2021-02-01 16:22:57 PST
<rdar://problem/73852354>
Comment 4 Ryosuke Niwa 2021-02-04 17:50:03 PST
As far as we've tested, there were no improvements on Intel Macs.