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.js 琐碎 #40

Open
juzhiyuan opened this issue Jul 7, 2018 · 0 comments
Open

Node.js 琐碎 #40

juzhiyuan opened this issue Jul 7, 2018 · 0 comments
Labels

Comments

@juzhiyuan
Copy link
Owner

juzhiyuan commented Jul 7, 2018

Node.js 擅长与不擅长的

擅长

  • I/O 密集型应用:因为 Node.js 以异步的方式处理 I/O 请求

不擅长

  • CPU(计算) 密集型:因为 Node.js 运行于单线程
for (let i = 0; i < 10000000000; i++) {
  console.log(i)
}

什么是异步

// 第一步:定义变量
let a = 1;
 
// 第二步:发出指令,然后把回调函数加入异步队列(回调函数并没有执行)
setTimeout(() => {
 console.log(a);
}, 0)

// 第三步:赋值,回调函数没有执行
a = 2;

// 第四步:发出指令,然后把回调函数加入异步队列(回调函数并没有执行)
setTimeout(() => {
 console.log(a);
}, 0)

// 第五步:赋值,回调函数没有执行
a = 3;

// 当所有代码执行完毕,cpu空闲下来了,就会开始执行异步队列里面的回调函数
// 所以最后控制台输出:3 3
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

1 participant