{"id":189,"date":"2026-06-29T10:33:06","date_gmt":"2026-06-29T02:33:06","guid":{"rendered":"https:\/\/yykcj.com\/?p=189"},"modified":"2026-06-29T10:33:06","modified_gmt":"2026-06-29T02:33:06","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-2","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=189","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\u3001\u975e\u963b\u585e I\/O \u6a21\u578b\uff0c\u5728\u6784\u5efa\u9ad8\u5e76\u53d1\u5e94\u7528\u65f6\u8868\u73b0\u51fa\u8272\u3002\u7136\u800c\uff0c\u968f\u7740\u4e1a\u52a1\u590d\u6742\u5ea6\u63d0\u5347\uff0c\u6027\u80fd\u95ee\u9898\u5f80\u5f80\u6084\u7136\u800c\u81f3\uff1a\u54cd\u5e94\u53d8\u6162\u3001CPU \u98d9\u5347\u3001\u5185\u5b58\u6cc4\u6f0f\u2026\u2026 \u5f88\u591a\u5f00\u53d1\u8005\u9762\u5bf9\u8fd9\u4e9b\u95ee\u9898\u65f6\u675f\u624b\u65e0\u7b56\uff0c\u53ea\u80fd\u76f2\u76ee\u52a0\u673a\u5668\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ece\u5b9e\u6218\u51fa\u53d1\uff0c\u5e26\u4f60\u638c\u63e1\u4e00\u5957\u5b8c\u6574\u7684\u6027\u80fd\u8c03\u4f18\u65b9\u6cd5\u8bba\uff1a<strong>\u8bca\u65ad \u2192 \u5206\u6790 \u2192 \u4f18\u5316 \u2192 \u9a8c\u8bc1<\/strong>\u3002\u6211\u4eec\u4f1a\u7528\u771f\u5b9e\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5b9a\u4f4d\u5e76\u89e3\u51b3\u5e38\u89c1\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<h2>1. \u6027\u80fd\u8bca\u65ad\u5de5\u5177\u94fe<\/h2>\n<p>\u5728\u4f18\u5316\u4e4b\u524d\uff0c\u5fc5\u987b\u77e5\u9053\u74f6\u9888\u5728\u54ea\u3002\u63a8\u8350\u4ee5\u4e0b\u5de5\u5177\uff1a<\/p>\n<ul>\n<li><strong>clinic.js<\/strong>: \u4e00\u7ad9\u5f0f\u6027\u80fd\u8bca\u65ad\u5de5\u5177\uff0c\u5305\u542b doctor\u3001bubbleprof\u3001flame \u4e09\u4e2a\u5b50\u547d\u4ee4\u3002<\/li>\n<li><strong>0x<\/strong>: \u706b\u7130\u56fe\u751f\u6210\u5668\uff0c\u76f4\u89c2\u5c55\u793a CPU \u70ed\u70b9\u3002<\/li>\n<li><strong>node &#8211;inspect<\/strong>: Chrome DevTools \u8c03\u8bd5\uff0c\u9002\u5408\u5185\u5b58\u5206\u6790\u3002<\/li>\n<\/ul>\n<h3>\u5b89\u88c5\u5de5\u5177<\/h3>\n<pre><code class=\"language-bash\">\nnpm install -g clinic 0x\n<\/code><\/pre>\n<h2>2. \u5b9e\u6218\u6848\u4f8b\uff1a\u4e00\u4e2a\u6162\u901f API<\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a Express \u5e94\u7528\uff0c<code>\/api\/users<\/code> \u63a5\u53e3\u54cd\u5e94\u7f13\u6162\uff08>5s\uff09\u3002\u4ee3\u7801\u7b80\u5316\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst express = require(&#x27;express&#x27;);\nconst app = express();\n\napp.get(&#x27;\/api\/users&#x27;, async (req, res) =&gt; {\n  const users = await getUsersFromDB();\n  const enrichedUsers = users.map(user =&gt; enrichUserData(user));\n  res.json(enrichedUsers);\n});\n\nasync function getUsersFromDB() {\n  \/\/ \u6a21\u62df\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u6bcf\u6b21\u67e5\u8be2\u8017\u65f6 1s\n  return new Promise(resolve =&gt; {\n    setTimeout(() =&gt; {\n      resolve([{ id: 1, name: &#x27;Alice&#x27; }, { id: 2, name: &#x27;Bob&#x27; }]);\n    }, 1000);\n  });\n}\n\nfunction enrichUserData(user) {\n  \/\/ \u6a21\u62df CPU \u5bc6\u96c6\u578b\u64cd\u4f5c\uff1a\u8ba1\u7b97\u7528\u6237\u6570\u636e\u7684\u54c8\u5e0c\n  let hash = 0;\n  for (let i = 0; i &lt; 1000000; i++) {\n    hash = (hash * 31 + user.name.charCodeAt(i % user.name.length)) | 0;\n  }\n  return { ...user, hash };\n}\n\napp.listen(3000);\n<\/code><\/pre>\n<p>\u542f\u52a8\u5e94\u7528\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnode app.js\n<\/code><\/pre>\n<p>\u7528 <code>clinic doctor<\/code> \u8bca\u65ad\uff1a<\/p>\n<pre><code class=\"language-bash\">\nclinic doctor -- node app.js\n<\/code><\/pre>\n<p>\u8bbf\u95ee <code>http:\/\/localhost:3000\/api\/users<\/code> \u51e0\u6b21\uff0c\u7136\u540e\u6309 <code>Ctrl+C<\/code> \u505c\u6b62\u3002clinic \u4f1a\u751f\u6210\u4e00\u4e2a HTML \u62a5\u544a\u3002<\/p>\n<h2>3. \u5206\u6790\u8bca\u65ad\u62a5\u544a<\/h2>\n<p>\u6253\u5f00\u62a5\u544a\uff0c\u4f60\u4f1a\u770b\u5230\uff1a<\/p>\n<ul>\n<li><strong>Event Loop \u5ef6\u8fdf<\/strong>\uff1a\u6307\u6807\u98d9\u5347\uff0c\u8bf4\u660e\u4e8b\u4ef6\u5faa\u73af\u88ab\u963b\u585e\u3002<\/li>\n<li><strong>CPU \u4f7f\u7528\u7387<\/strong>\uff1a\u63a5\u8fd1 100%\uff0c\u4f46 I\/O \u7b49\u5f85\u65f6\u95f4\u5f88\u4f4e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u8bf4\u660e\u74f6\u9888\u5728 <strong>CPU \u5bc6\u96c6\u8ba1\u7b97<\/strong> \u800c\u975e I\/O\u3002<\/p>\n<p>\u8fdb\u4e00\u6b65\u7528 <code>clinic flame<\/code> \u751f\u6210\u706b\u7130\u56fe\uff1a<\/p>\n<pre><code class=\"language-bash\">\nclinic flame -- node app.js\n<\/code><\/pre>\n<p>\u706b\u7130\u56fe\u4e2d\uff0c<code>enrichUserData<\/code> \u51fd\u6570\u5360\u636e\u4e86\u7edd\u5927\u90e8\u5206\u5bbd\u5ea6\uff0c\u786e\u8ba4\u662f\u70ed\u70b9\u3002<\/p>\n<h2>4. \u4f18\u5316\u7b56\u7565<\/h2>\n<h3>4.1 \u5f02\u6b65\u975e\u963b\u585e\u4f18\u5316<\/h3>\n<p>\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0cNode.js \u7684\u4e3b\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\u3002\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u5176\u5206\u79bb\u5230 <strong>Worker Threads<\/strong> \u6216\u5b50\u8fdb\u7a0b\u3002<\/p>\n<h4>\u4f7f\u7528 Worker Threads<\/h4>\n<p>\u521b\u5efa <code>worker.js<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst { parentPort } = require(&#x27;worker_threads&#x27;);\n\nparentPort.on(&#x27;message&#x27;, (user) =&gt; {\n  let hash = 0;\n  for (let i = 0; i &lt; 1000000; i++) {\n    hash = (hash * 31 + user.name.charCodeAt(i % user.name.length)) | 0;\n  }\n  parentPort.postMessage({ ...user, hash });\n});\n<\/code><\/pre>\n<p>\u4fee\u6539\u4e3b\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst { Worker } = require(&#x27;worker_threads&#x27;);\n\napp.get(&#x27;\/api\/users&#x27;, async (req, res) =&gt; {\n  const users = await getUsersFromDB();\n  const enrichedUsers = await Promise.all(users.map(user =&gt; {\n    return new Promise((resolve, reject) =&gt; {\n      const worker = new Worker(&#x27;.\/worker.js&#x27;);\n      worker.postMessage(user);\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  res.json(enrichedUsers);\n});\n<\/code><\/pre>\n<p>> \ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a\u9891\u7e41\u521b\u5efa Worker \u6709\u5f00\u9500\uff0c\u53ef\u8003\u8651\u4f7f\u7528 Worker \u6c60\uff08\u5982 <code>workerpool<\/code> \u5e93\uff09\u3002<\/p>\n<h3>4.2 \u4f7f\u7528\u7f13\u5b58\u51cf\u5c11\u8ba1\u7b97<\/h3>\n<p>\u5982\u679c <code>enrichUserData<\/code> \u7684\u7ed3\u679c\u53ef\u590d\u7528\uff0c\u52a0\u4e00\u5c42\u7f13\u5b58\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst NodeCache = require(&#x27;node-cache&#x27;);\nconst cache = new NodeCache({ stdTTL: 600 });\n\nfunction enrichUserData(user) {\n  const cacheKey = `enrich_${user.id}`;\n  let cached = cache.get(cacheKey);\n  if (cached) return cached;\n  \/\/ \u8ba1\u7b97...\n  cache.set(cacheKey, enriched);\n  return enriched;\n}\n<\/code><\/pre>\n<h3>4.3 \u4f7f\u7528\u539f\u751f\u6a21\u5757\u6216 C++ \u63d2\u4ef6<\/h3>\n<p>\u5bf9\u4e8e\u6781\u7aef\u8ba1\u7b97\u573a\u666f\uff0c\u53ef\u7528 <code>node-addon-api<\/code> \u7f16\u5199 C++ \u63d2\u4ef6\uff0c\u6216\u4f7f\u7528\u73b0\u6709\u7684\u9ad8\u6027\u80fd\u5e93\u5982 <code>bcrypt<\/code>\uff08\u5df2\u539f\u751f\u5b9e\u73b0\uff09\u3002<\/p>\n<h2>5. \u5185\u5b58\u6cc4\u6f0f\u6392\u67e5<\/h2>\n<p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u5185\u5b58\u6cc4\u6f0f\u3002\u7528 <code>node --inspect<\/code> \u542f\u52a8\u5e94\u7528\uff0c\u6253\u5f00 Chrome DevTools \u7684 Memory \u9762\u677f\uff0c\u6293\u53d6\u5806\u5feb\u7167\u3002<\/p>\n<p>\u5e38\u89c1\u6cc4\u6f0f\u539f\u56e0\uff1a<\/p>\n<ul>\n<li>\u5168\u5c40\u53d8\u91cf\u672a\u6e05\u7406<\/li>\n<li>\u95ed\u5305\u5f15\u7528<\/li>\n<li>\u4e8b\u4ef6\u76d1\u542c\u5668\u672a\u79fb\u9664<\/li>\n<\/ul>\n<h2>6. \u9a8c\u8bc1\u4f18\u5316\u6548\u679c<\/h2>\n<p>\u518d\u6b21\u8fd0\u884c <code>clinic doctor<\/code>\uff0c\u89c2\u5bdf\u4e8b\u4ef6\u5faa\u73af\u5ef6\u8fdf\u548c CPU \u4f7f\u7528\u7387\u3002\u4f18\u5316\u540e\uff0c\u5ef6\u8fdf\u5e94\u5927\u5e45\u4e0b\u964d\uff0cCPU \u4f7f\u7528\u7387\u66f4\u5747\u8861\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Node.js \u6027\u80fd\u8c03\u4f18\u7684\u6838\u5fc3\u662f <strong>\u8bc6\u522b\u74f6\u9888\u7c7b\u578b<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>I\/O \u5bc6\u96c6\u578b<\/strong>\uff1a\u4f7f\u7528\u5f02\u6b65\u3001\u8fde\u63a5\u6c60\u3001\u8d1f\u8f7d\u5747\u8861\u3002<\/li>\n<li><strong>CPU \u5bc6\u96c6\u578b<\/strong>\uff1a\u4f7f\u7528 Worker Threads\u3001\u5b50\u8fdb\u7a0b\u3001\u7f13\u5b58\u3001\u539f\u751f\u6a21\u5757\u3002<\/li>\n<li><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong>\uff1a\u5584\u7528\u5806\u5feb\u7167\u5206\u6790\u3002<\/li>\n<\/ul>\n<p>\u63a8\u8350\u5de5\u5177\u94fe\uff1a<code>clinic.js<\/code> + <code>0x<\/code> + Chrome DevTools\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/nodejs.org\/en\/docs\/guides\/diagnostics\/\">Node.js \u5b98\u65b9\u6027\u80fd\u6307\u5357<\/a><\/li>\n<li><a href=\"https:\/\/clinicjs.org\/documentation\/\">clinic.js \u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/nodejs.org\/api\/worker_threads.html\">Worker Threads \u6700\u4f73\u5b9e\u8df5<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u901a\u8fc7\u771f\u5b9e\u6848\u4f8b\uff0c\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528 clinic.js\u30010x \u7b49\u5de5\u5177\u8bca\u65ad Node.js \u5e94\u7528\u6027\u80fd\u74f6\u9888\uff0c\u5e76\u7ed9\u51fa\u5f02\u6b65\u4f18\u5316\u3001\u5185\u5b58\u7ba1\u7406\u3001CPU \u5bc6\u96c6\u4efb\u52a1\u5904\u7406\u7b49\u5b9e\u6218\u6280\u5de7\uff0c\u52a9\u4f60\u5feb\u901f\u63d0\u5347\u5e94\u7528\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,5,87,8,6],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-clinic-js","tag-node-js","tag-worker-threads","tag-8","tag-6"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/189","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=189"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}