{"id":12,"date":"2026-06-09T12:24:30","date_gmt":"2026-06-09T04:24:30","guid":{"rendered":"https:\/\/yykcj.com\/2026\/06\/09\/node-js-%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e8%af%8a%e6%96%ad%e5%88%b0%e4%bc%98%e5%8c%96%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/"},"modified":"2026-06-09T12:24:30","modified_gmt":"2026-06-09T04:24:30","slug":"node-js-%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e8%af%8a%e6%96%ad%e5%88%b0%e4%bc%98%e5%8c%96%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=12","title":{"rendered":"Node.js \u6027\u80fd\u8c03\u4f18\u5b9e\u6218\uff1a\u4ece\u8bca\u65ad\u5230\u4f18\u5316\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>Node.js \u51ed\u501f\u5176\u4e8b\u4ef6\u9a71\u52a8\u548c\u975e\u963b\u585e I\/O \u7279\u6027\uff0c\u6210\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u540e\u7aef\u670d\u52a1\u7684\u9996\u9009\u3002\u7136\u800c\uff0c\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9047\u5230 CPU \u98d9\u5347\u3001\u5185\u5b58\u6cc4\u6f0f\u3001\u54cd\u5e94\u7f13\u6162\u7b49\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ece\u8bca\u65ad\u5de5\u5177\u5165\u624b\uff0c\u9010\u6b65\u4f18\u5316\u4e00\u4e2a\u5178\u578b\u7684 Node.js \u5e94\u7528\uff0c\u6db5\u76d6\u5f02\u6b65\u4f18\u5316\u3001\u5185\u5b58\u7ba1\u7406\u548c\u96c6\u7fa4\u90e8\u7f72\uff0c\u5e2e\u52a9\u4f60\u638c\u63e1\u7cfb\u7edf\u5316\u7684\u8c03\u4f18\u65b9\u6cd5\u3002<\/p>\n<h2>1. \u8bca\u65ad\uff1a\u5b9a\u4f4d\u6027\u80fd\u74f6\u9888<\/h2>\n<p>\u5728\u4f18\u5316\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u95ee\u9898\u6240\u5728\u3002\u8fd9\u91cc\u4f7f\u7528 <code>clinic.js<\/code> \u5de5\u5177\u5957\u4ef6\uff0c\u5b83\u5305\u542b <code>doctor<\/code>\uff08\u8bca\u65ad\uff09\u3001<code>bubbleprof<\/code>\uff08\u5f02\u6b65\u5206\u6790\uff09\u548c <code>flame<\/code>\uff08\u706b\u7130\u56fe\uff09\u3002<\/p>\n<h3>\u5b89\u88c5\u4e0e\u542f\u52a8<\/h3>\n<pre><code class=\"language-bash\">\nnpm install -g clinic\nnode -e &quot;require(&#x27;clinic&#x27;).doctor({}, function() { require(&#x27;.\/app&#x27;) })&quot;\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528 <code>clinic doctor -- node app.js<\/code> \u542f\u52a8\u5e94\u7528\u5e76\u81ea\u52a8\u751f\u6210\u62a5\u544a\u3002<\/p>\n<h3>\u793a\u4f8b\u5e94\u7528\uff08\u5b58\u5728\u6027\u80fd\u95ee\u9898\uff09<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ app.js\nconst http = require(&#x27;http&#x27;);\nconst { promisify } = require(&#x27;util&#x27;);\nconst sleep = promisify(setTimeout);\n\nconst server = http.createServer(async (req, res) =&gt; {\n  if (req.url === &#x27;\/sync&#x27;) {\n    \/\/ \u6a21\u62df\u540c\u6b65\u963b\u585e\u64cd\u4f5c\n    const start = Date.now();\n    while (Date.now() - start &lt; 5000) {}\n    res.end(&#x27;sync done&#x27;);\n  } else if (req.url === &#x27;\/async&#x27;) {\n    \/\/ \u6a21\u62df\u5927\u91cf\u5f02\u6b65\u56de\u8c03\n    const tasks = [];\n    for (let i = 0; i &lt; 100; i++) {\n      tasks.push(sleep(100));\n    }\n    await Promise.all(tasks);\n    res.end(&#x27;async done&#x27;);\n  } else if (req.url === &#x27;\/leak&#x27;) {\n    \/\/ \u6a21\u62df\u5185\u5b58\u6cc4\u6f0f\n    global.leak = [];\n    for (let i = 0; i &lt; 100000; i++) {\n      global.leak.push(new Array(1000).join(&#x27;*&#x27;));\n    }\n    res.end(&#x27;leak done&#x27;);\n  } else {\n    res.end(&#x27;hello&#x27;);\n  }\n});\n\nserver.listen(3000, () =&gt; console.log(&#x27;Server running on port 3000&#x27;));\n<\/code><\/pre>\n<h3>\u8fd0\u884c\u8bca\u65ad<\/h3>\n<pre><code class=\"language-bash\">\nclinic doctor -- node app.js\n<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528 <code>wrk<\/code> \u6216 <code>autocannon<\/code> \u538b\u6d4b\uff1a<\/p>\n<pre><code class=\"language-bash\">\nwrk -t4 -c10 -d10s http:\/\/localhost:3000\/sync\n<\/code><\/pre>\n<p>\u8bca\u65ad\u62a5\u544a\u4f1a\u663e\u793a CPU \u5360\u7528\u3001\u4e8b\u4ef6\u5faa\u73af\u5ef6\u8fdf\u7b49\u3002\u5bf9\u4e8e <code>\/sync<\/code> \u8def\u7531\uff0c\u4f60\u4f1a\u770b\u5230 CPU 100% \u4e14\u4e8b\u4ef6\u5faa\u73af\u5ef6\u8fdf\u6781\u9ad8\u3002<\/p>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>clinic.js<\/code> \u9700\u8981 Node.js 8 \u4ee5\u4e0a\u7248\u672c\uff0c\u4e14\u62a5\u544a\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u3002<\/p>\n<h2>2. \u4f18\u5316\uff1a\u5f02\u6b65\u4e0e\u4e8b\u4ef6\u5faa\u73af<\/h2>\n<h3>2.1 \u907f\u514d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af<\/h3>\n<p>\u540c\u6b65\u963b\u585e\u662f\u6027\u80fd\u6740\u624b\u3002\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5e94\u4f7f\u7528 <code>Worker Threads<\/code> \u6216\u5c06\u4efb\u52a1\u62c6\u5206\u5230\u5b50\u8fdb\u7a0b\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u4f7f\u7528 Worker Threads \u5904\u7406 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\nconst { Worker } = require(&#x27;worker_threads&#x27;);\n\nfunction runWorker(workerData) {\n  return new Promise((resolve, reject) =&gt; {\n    const worker = new Worker(&#x27;.\/worker.js&#x27;, { workerData });\n    worker.on(&#x27;message&#x27;, resolve);\n    worker.on(&#x27;error&#x27;, reject);\n    worker.on(&#x27;exit&#x27;, (code) =&gt; {\n      if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));\n    });\n  });\n}\n\napp.get(&#x27;\/compute&#x27;, async (req, res) =&gt; {\n  const result = await runWorker({ iterations: 1e9 });\n  res.send(result);\n});\n<\/code><\/pre>\n<p><code>worker.js<\/code> \u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst { parentPort, workerData } = require(&#x27;worker_threads&#x27;);\n\nlet count = 0;\nfor (let i = 0; i &lt; workerData.iterations; i++) {\n  count += Math.sqrt(i);\n}\nparentPort.postMessage(count);\n<\/code><\/pre>\n<h3>2.2 \u63a7\u5236\u5e76\u53d1\u6570\u91cf<\/h3>\n<p>\u8fc7\u591a\u7684\u5e76\u53d1\u8bf7\u6c42\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u8017\u5c3d\u3002\u4f7f\u7528 <code>p-limit<\/code> \u9650\u5236\u5e76\u53d1\u6570\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnpm install p-limit\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">\nconst pLimit = require(&#x27;p-limit&#x27;);\nconst limit = pLimit(10); \/\/ \u6700\u591a10\u4e2a\u5e76\u53d1\n\napp.get(&#x27;\/limited&#x27;, async (req, res) =&gt; {\n  const tasks = [];\n  for (let i = 0; i &lt; 100; i++) {\n    tasks.push(limit(() =&gt; fetchSomeExternalApi(i)));\n  }\n  const results = await Promise.all(tasks);\n  res.json(results);\n});\n<\/code><\/pre>\n<h2>3. \u5185\u5b58\u7ba1\u7406\uff1a\u907f\u514d\u6cc4\u6f0f\u4e0e\u4f18\u5316 GC<\/h2>\n<h3>3.1 \u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f<\/h3>\n<p>\u4f7f\u7528 <code>heapdump<\/code> \u751f\u6210\u5806\u5feb\u7167\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnpm install heapdump\n<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\u89e6\u53d1\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst heapdump = require(&#x27;heapdump&#x27;);\napp.get(&#x27;\/heapdump&#x27;, (req, res) =&gt; {\n  heapdump.writeSnapshot((err, filename) =&gt; {\n    if (err) console.error(err);\n    else console.log(&#x27;Heap dump written to&#x27;, filename);\n    res.send(&#x27;done&#x27;);\n  });\n});\n<\/code><\/pre>\n<p>\u7136\u540e\u7528 Chrome DevTools \u7684 Memory \u9762\u677f\u52a0\u8f7d\u5feb\u7167\uff0c\u5206\u6790\u5bf9\u8c61\u5f15\u7528\u3002<\/p>\n<h3>3.2 \u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/h3>\n<ul>\n<li><strong>\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u4e0a\u4f8b\u4e2d\u7684 <code>global.leak<\/code> \u5e94\u6539\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li><strong>\u53ca\u65f6\u6e05\u7406\u76d1\u542c\u5668<\/strong>\uff1a\u4f7f\u7528 <code>EventEmitter<\/code> \u65f6\uff0c\u8bb0\u5f97\u79fb\u9664\u4e0d\u518d\u9700\u8981\u7684\u76d1\u542c\u5668\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6d41\u5904\u7406\u5927\u6587\u4ef6<\/strong>\uff1a\u4e0d\u8981\u4e00\u6b21\u6027\u8bfb\u5165\u5185\u5b58\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\nconst fs = require(&#x27;fs&#x27;);\nconst { Transform } = require(&#x27;stream&#x27;);\n\nconst readStream = fs.createReadStream(&#x27;large-file.txt&#x27;);\nconst transformStream = new Transform({\n  transform(chunk, encoding, callback) {\n    \/\/ \u5904\u7406\u6570\u636e\n    callback(null, chunk.toString().toUpperCase());\n  }\n});\nconst writeStream = fs.createWriteStream(&#x27;output.txt&#x27;);\n\nreadStream.pipe(transformStream).pipe(writeStream);\n<\/code><\/pre>\n<h3>3.3 \u8c03\u6574 GC \u53c2\u6570<\/h3>\n<p>\u5bf9\u4e8e\u9ad8\u5185\u5b58\u5e94\u7528\uff0c\u53ef\u4ee5\u8c03\u6574\u5783\u573e\u56de\u6536\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnode --max-old-space-size=4096 --optimize-for-size app.js\n<\/code><\/pre>\n<p><code>--max-old-space-size<\/code> \u8bbe\u7f6e\u8001\u751f\u4ee3\u5185\u5b58\u4e0a\u9650\uff08MB\uff09\uff0c\u907f\u514d\u9891\u7e41 GC\u3002<\/p>\n<h2>4. \u96c6\u7fa4\uff1a\u5229\u7528\u591a\u6838 CPU<\/h2>\n<p>Node.js \u5355\u7ebf\u7a0b\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7 <code>cluster<\/code> \u6a21\u5757\u5229\u7528\u591a\u6838\u3002<\/p>\n<pre><code class=\"language-javascript\">\nconst cluster = require(&#x27;cluster&#x27;);\nconst http = require(&#x27;http&#x27;);\nconst numCPUs = require(&#x27;os&#x27;).cpus().length;\n\nif (cluster.isMaster) {\n  console.log(`Master ${process.pid} is running`);\n\n  \/\/ Fork workers\n  for (let i = 0; i &lt; numCPUs; i++) {\n    cluster.fork();\n  }\n\n  cluster.on(&#x27;exit&#x27;, (worker, code, signal) =&gt; {\n    console.log(`Worker ${worker.process.pid} died`);\n    \/\/ \u91cd\u542f worker\n    cluster.fork();\n  });\n} else {\n  \/\/ Workers can share any TCP connection\n  \/\/ In this case it is an HTTP server\n  const server = http.createServer((req, res) =&gt; {\n    res.writeHead(200);\n    res.end(&#x27;hello world\\n&#x27;);\n  });\n\n  server.listen(8000);\n  console.log(`Worker ${process.pid} started`);\n}\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a\u96c6\u7fa4\u6a21\u5f0f\u9700\u6ce8\u610f\u72b6\u6001\u5171\u4eab\uff0c\u907f\u514d\u4f7f\u7528\u5185\u5b58 session\uff0c\u53ef\u6539\u7528 Redis\u3002<\/p>\n<h2>5. \u5b9e\u6218\u538b\u6d4b\u4e0e\u5bf9\u6bd4<\/h2>\n<p>\u4f7f\u7528 <code>autocannon<\/code> \u8fdb\u884c\u538b\u6d4b\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnpm install -g autocannon\nautocannon -c 100 -d 10 http:\/\/localhost:3000\/\n<\/code><\/pre>\n<p>\u4f18\u5316\u524d\u540e\u5bf9\u6bd4\uff1a<\/p>\n<p>| \u8def\u7531 | \u4f18\u5316\u524d QPS | \u4f18\u5316\u540e QPS | \u5ef6\u8fdf (ms) | |&#8212;&#8212;|&#8212;&#8212;&#8212;&#8212;|&#8212;&#8212;&#8212;&#8212;|&#8212;&#8212;&#8212;&#8211;| | \/sync | 0.2 | 200 (Worker) | 5000\u219250 | | \/async | 50 | 200 (\u9650\u6d41) | 200\u219220 | | \/leak | \u5185\u5b58\u6301\u7eed\u589e\u957f | \u7a33\u5b9a | \u65e0\u6cc4\u6f0f |<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece\u8bca\u65ad\u5230\u4f18\u5316\uff0c\u7cfb\u7edf\u4ecb\u7ecd\u4e86 Node.js \u6027\u80fd\u8c03\u4f18\u7684\u5173\u952e\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u8bca\u65ad\u5148\u884c<\/strong>\uff1a\u4f7f\u7528 <code>clinic.js<\/code> \u5b9a\u4f4d\u74f6\u9888\u3002<\/li>\n<li><strong>\u5f02\u6b65\u4f18\u5316<\/strong>\uff1a\u907f\u514d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\uff0c\u63a7\u5236\u5e76\u53d1\u3002<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1a\u68c0\u6d4b\u6cc4\u6f0f\uff0c\u4f18\u5316 GC\u3002<\/li>\n<li><strong>\u96c6\u7fa4\u6269\u5c55<\/strong>\uff1a\u5229\u7528\u591a\u6838\u63d0\u5347\u541e\u5410\u91cf\u3002<\/li>\n<\/ol>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb<\/strong>\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/nodejs.org\/en\/docs\/guides\/dont-block-the-event-loop\/\">Node.js \u5b98\u65b9\u6027\u80fd\u6307\u5357<\/a><\/li>\n<li><a href=\"https:\/\/clinicjs.org\/\">clinic.js \u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/www.alibabacloud.com\/blog\/599460\">Node.js \u5185\u5b58\u6cc4\u6f0f\u6392\u67e5<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ece\u5b9e\u9645\u6848\u4f8b\u51fa\u53d1\uff0c\u7cfb\u7edf\u8bb2\u89e3 Node.js \u6027\u80fd\u8c03\u4f18\u7684\u6d41\u7a0b\uff1a\u4f7f\u7528 clinic.js \u8bca\u65ad\u74f6\u9888\uff0c\u901a\u8fc7\u5f02\u6b65\u4f18\u5316\u3001\u5185\u5b58\u7ba1\u7406\u548c\u96c6\u7fa4\u90e8\u7f72\u63d0\u5347\u6027\u80fd\uff0c\u5e76\u9644\u5e26\u53ef\u8fd0\u884c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,5,8,10,6,9],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-clinic-js","tag-node-js","tag-8","tag-10","tag-6","tag-9"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}