# nodeTest **Repository Path**: loveyingshi/nodeTest ## Basic Information - **Project Name**: nodeTest - **Description**: node的学习之旅 - **Primary Language**: NodeJS - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-05-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # node 学习之旅 ## node node 内部原理 ### 1.event loop 在 JavaScript 中,任务被分为 Task(又称为 MacroTask,宏任务)和 MicroTask(微任务)两种 - MacroTask: script(整体代码), setTimeout, setInterval, setImmediate(node 独有), I/O, UI rendering - MicroTask: process.nextTick(node 独有), Promises, Object.observe(废弃), MutationObserver` 执行顺序为同步代码—>MicroTask>MacroTask ### 2.thread pool 默认的线程池 process.env.UV_THREADPOOL_SIZE 大小为 4, 运行速度主要针对 cpu 是几核 运行 node threads.js ``` const crypto = require("crypto"); const start = Date.now(); crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("1:", Date.now() - start); }); crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("2:", Date.now() - start); }); crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("3:", Date.now() - start); }); crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("4:", Date.now() - start); }); crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("5:", Date.now() - start); }); //console.log 4: 871 2: 885 1: 892 3: 908 5: 1719 ``` ### 3.httpRequest node async.js libUv 将网络请求委托给操作系统 因此 JavaScript 代码并没有阻塞 ``` const https = require("https"); const start = Date.now(); function doRequest() { https .request("https://www.bing.com", res => { res.on("data", () => {}); res.on("end", () => { console.log(Date.now() - start); }); }) .end(); } doRequest(); doRequest(); doRequest(); doRequest(); doRequest(); doRequest(); doRequest(); //result 213 215 216 221 224 228 230 ``` ### 总结 fs 模块和加密模块共同使用线程池 fs 模块读取文件后,会存储起来并暂停挂起,等待线程池的线程空余出来后,再进行处理 httpRequest 不使用线程池,而是委托给操作系统 node multitask.js ``` const https = require("https"); const crypto = require("crypto"); const fs = require("fs"); const start = Date.now(); function doRequest() { https .request("https://www.bing.com", res => { res.on("data", () => {}); res.on("end", () => { console.log(Date.now() - start); }); }) .end(); } function doHash() { crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => { console.log("Hash:", Date.now() - start); }); } doRequest(); fs.readFile("multitask.js", "utf8", () => { console.log("FS:", Date.now() - start); }); doHash(); doHash(); doHash(); doHash(); // result 211 Hash: 874 FS: 874 Hash: 878 Hash: 878 Hash: 886 ``` ## Node performance ### 1.single thread 的坏处(处理 CPU 密集型吃力) 当某个请求中含有阻塞性的 JavaScript 代码时,是灾难性的 如浏览器访问该接口,需要等待处理完请求才能继续接下来的操作 ``` const express = require("express"); const app = express(); function doWork(duration) { const start = Date.now(); while (Date.now() - start < duration) {} } app.get("/", (req, res) => { doWork(5000); res.send("hi there"); }); app.listen(5000); // node index.js ``` ### 2.cluster manager 为了充分利用多核系统 cluster 模块可以创建共享服务器端口的子进程。 然而并不是 cluster.fork()越多多好 ### pm2 PM2 是一个带有负载均衡功能的 Node 应用进程管理器 内建负载均衡(使用 Node cluster 集群模块) ``` pm2 start index.js -i 0 pm2 list pm2 delete index ``` ### webworker-threads