Home>Article>Web Front-end> A closer look at non-blocking I/O in Node.js
Speaking ofNode.js, you have probably heard a bunch of dizzying terms
About asynchronous, mainlyEvent loopand **Non-blocking I/O, **officially because of these two points, Node.js can be called high performance.
So it is very important to understand the asynchronous mechanism and usage of Node.js
The difference between blocking I/O and non-blocking I/O It depends on the time it takes for the system to receive input and then output, and whether it can receive other inputs
The situation of blocking I/O is-> He keeps waiting for other people to answer, and when other people answer, he does other things.
The situation with non-blocking I/O is-> He goes to do other things, and then comes back to see the answer after a while
Example of doing houseworkconst glob = require("glob"); var result = null; console.time("glob"); // 获取目下的文件和文件名 result = glob.sync(__dirname + "/**/*"); console.timeEnd("glob"); console.log(result);Print the following results
You can see that it takes
30 milliseconds of waiting timeto get the resultIt can be concluded from the above code that
Understanding non-blocking I/O from the code
const glob = require("glob"); var result = null; console.time("glob"); // 获取目下的文件和文件名 glob(__dirname + "/**/*", function (err, res) { result = res; console.log("got result"); }); console.timeEnd("glob"); console.log(1 + 1);Print the following results
glob: 3.198ms 2 got result
It can be concluded from the above code that
non-blocking I/O reduces the waiting time , you can also perform other things during the execution processFor more programming-related knowledge, please visit:
Introduction to ProgrammingThe above is the detailed content of A closer look at non-blocking I/O in Node.js. For more information, please follow other related articles on the PHP Chinese website!