Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[node]关于setImmediate和process.nextTick的区别 #2

Open
VaJoy opened this issue Dec 9, 2015 · 3 comments
Open

[node]关于setImmediate和process.nextTick的区别 #2

VaJoy opened this issue Dec 9, 2015 · 3 comments
Labels

Comments

@VaJoy
Copy link
Member

VaJoy commented Dec 9, 2015

setImmediateprocess.nextTick都会在下一个 event loop 时执行,但是它们有如下区别:

  1. setImmediate不会call stack,可以用于大规模异步递归调用。而process.nextTick受到 maxTickDepth(默认为1000) 的限制,超过其数值会出错。

setImmediate(handler) 并不像 setTimeout(handler, 0) 由event loop检测系统时间是否到点然后向事件队列插入一个事件,然后调用事件的回调方法handler。而是监控UI线程的调用栈,一旦调用栈为空则将handler压栈。

  1. setImmediate可视为“排队等候的人”,而 process.nextTick 是“插队的人”,其区别可见下图:
    区别
    其中“等待队列”可以是诸如A事件之前触发的 setTimeout 时间队列
@coxo
Copy link
Member

coxo commented Dec 9, 2015

引用:process.nextTick()属于idle观察者,setImmediate()属于check观察者.在每一轮循环检查中,idle观察者先于I/O观察者,I/O观察者先于check观察者.

@VaJoy VaJoy added the node label Dec 11, 2015
@VaJoy
Copy link
Member Author

VaJoy commented Aug 5, 2017

补个图
image

@rijieli
Copy link

rijieli commented Dec 7, 2017

请教一下这个图用什么做的啊

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

No branches or pull requests

3 participants