Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

implement setImmediate #3845

Closed
wants to merge 3 commits into from
Closed

Conversation

tjfontaine
Copy link

This is a basic implementation of setImmediate from https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html

I followed the spirit of the async queue, yielding after each iteration. I did not implement the integer id but followed the opaque object from the other timer types.

Tests and documentation will be forthcoming.

};
L.init(_immediateQueue);

var processImmediate = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's never reassigned so use function processImmediate() { here.

@medikoo
Copy link

medikoo commented Aug 9, 2012

Shouldn't setImmediate be just alias of process.nextTick? What's the difference?

@tjfontaine
Copy link
Author

The semantics of process.nextTick have changed for 0.9 and forward (see #3723) so there is a need for something new.

The main difference between nextTick and setImmediate is that between each scheduled immediate that is processed it yields time back to the event loop until the timer is scheduled again, in this case the timer is 0 so it happens on the next trip through the event processing.

Another small difference is that you can pass arguments to the function provided

@isaacs
Copy link

isaacs commented Aug 9, 2012

Also, setImmediate returns a handle that can be passed to clearImmediate to
cancel it.

@medikoo
Copy link

medikoo commented Aug 10, 2012

Thanks for clarification! So I assume that apart from obvious API differences (arguments, and clear handle), difference lies in fact that setImmediate sits on top of timers (is handled by them) and nextTick is more low-level and is handled before and outside of timers logic.

I understand then, that on Node.js ground setImmediate(fn) would have exactly same effect as setTimeout(fn, 0)?

Do timers logic in some significant way delay execution of (what would be) setImmediate callback when compared to nextTick callback?

@tjfontaine
Copy link
Author

I will try and layout the differences of all the deferral mechanisms including the proposed setImmediate

  • process.nextTick
    • fired from tick spinner, no timer objects allocated
    • executes up to process.maxTickDepth queued callbacks before yielding to event loop
    • for 10 consecutive process.nextTick 0 allocated timers, 0 yields to event loop
  • setTimeout
    • All related idle times fall into a similar bucket, one timer allocated per bucket
    • fired from event loop
    • executes all queued callbacks for idle time without yielding to event loop
    • destroys timer
    • for 10 consecutive setTimeout(x, 0) 1 timer allocated, 0 yields to event loop
  • setInterval
    • timer object allocated per setInterval call, destroy'ed on clearInterval
    • each fired from event loop based on the loops scheduling
    • for 10 consecutive setInterval(x, 0) 10 timers allocated, 0 yields to event loop
  • setImmediate
    • single module scoped timer object allocated
    • after execution of each queued callback yield to event loop
    • for 10 consecutive setImmediate(x) 1 module allocated timer, 10 yields to event loop

immediate = L.shift(immediateQueue);

if (immediate.domain)
immediate.domain.enter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this one line. I say that because if I don't complain about it, @isaacs will - he is like that.

@bnoordhuis
Copy link
Member

Generally LGTM, both conceptually and implementation-wise, but will need tests and documentation, of course.

@isaacs Agreed?

@tjfontaine
Copy link
Author

I have added a test and documentation

@bnoordhuis
Copy link
Member

Thanks Timothy, landed in 382f22f.

@bnoordhuis bnoordhuis closed this Aug 11, 2012
@tjfontaine
Copy link
Author

thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants