Skip to content

setInterval interval includes duration of callback #7346

@distributedlife

Description

@distributedlife
  • Version: Multiple
  • Platform: Darwin 15.5.0 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64 x86_64

When using setInterval in node, I find that node waits until the callback is complete before scheduling the next interval. This appears to be different behaviour to setInterval on Chrome, Safari, Opera and Firefox. My expectation was that setInterval would try to schedule on the interval. At present I find the behaviour equivalent to setTimeout with automatic rescheduling.

I've read #5426 and #3063 and while the seem to relate, they refer to setTimeout and not to setInterval.

I've have test scenarios and results in this repository.

My test is to execute setInterval with a 100ms interval. The callback spends about 5-7ms calculating fibonacci. At the start of the callback I record the current time. I let the setInterval run for 10 seconds. Afterwards I calculate the delta between setInterval calls.

The aforementioned browsers have an average delta between 100-103ms. I don't expect the delta to be exactly 100ms.

When running on node 0.x, I get results of 106-107ms for the average delta. This is the interval plus the approximate duration of the callback. When running on node 4, 5 or 6, I get 112-114ms. About double the callback duration.

If I increase the workload of the fibonacci function from 2,000,000 to 20,000,000 cycles the browsers stay at ~100ms, while node 0.x jumps to ~130-140ms and node 4+ to ~180-190ms. With this we're executing 2 callbacks in node to every 3-4 in the browser.

If you want to run the tests you can clone set-interval-behaviour and run node index.js for node's behaviour and open index.html for browser behaviour.

I have been using nodenv to switch between different node versions.

Implementation Average Delta
node 6.2.1 113.79
node 5.11.1 114.58
node 4.4.5 112.83
node 0.12.14 107.28
node 0.10.45 106.51
Chrome 51.0.2704.84 100.02
Firefox 47.0 101.87
Firefox 45.0.2 103.36
Firefox 43.0.4 99.97
Opera 38.0.2220.31 100.03
Opera 37.0.2178.32 100.02
Safari 9.1.1 103.06

Metadata

Metadata

Assignees

No one assigned

    Labels

    timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions