node在执行IO操作(读取文件)时会开启线程池(默认是4个),咱们能够通过设置UV_THREADPOOL_SIZE减少线程池个数。

背景:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title></head><body>    </body></html>
const fs = require("fs");process.env.UV_THREADPOOL_SIZE=64;setInterval(() => {    fs.readFile(`${__dirname}/index.html`,()=>{        console.log("read success");    })}, 3000);
{  "name": "threadpool",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "start":"node index.js"  },  "keywords": [],  "author": "",  "license": "ISC"}

在windows下运行node,发现process.env.UV_THREADPOOL_SIZE设置有效。

线程数量应该从12减少到76个,理论为16个,阐明UV_THREADPOOL_SIZE设置有效

起因:
Linux零碎下能够在js代码里间接设置,而windows零碎下须要在执行node命令前设置线程池数量,将node启动改为如下图(注:=号左右不要有空格)

运行后果: