{"id":338564,"date":"2022-09-21T09:00:15","date_gmt":"2022-09-21T09:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338564"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338564","title":{"rendered":"<span>\u041f\u0440\u0438\u0440\u0443\u0447\u0430\u0435\u043c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 Node.js (\u0447\u0430\u0441\u0442\u044c 1: \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b)<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c40\/f06\/cb2\/c40f06cb2ffb0542374a702171c43838.jpeg\" alt=\"\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u0433\u0438\u0434\u0440\u0430 \u0431\u043e\u043b\u044c\u043d\u043e \u043a\u0443\u0441\u0430\u0435\u0442\u0441\u044f\" title=\"\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u0433\u0438\u0434\u0440\u0430 \u0431\u043e\u043b\u044c\u043d\u043e \u043a\u0443\u0441\u0430\u0435\u0442\u0441\u044f\" width=\"1332\" height=\"850\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c40\/f06\/cb2\/c40f06cb2ffb0542374a702171c43838.jpeg\" data-blurred=\"true\"\/><figcaption>\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u0433\u0438\u0434\u0440\u0430 \u0431\u043e\u043b\u044c\u043d\u043e \u043a\u0443\u0441\u0430\u0435\u0442\u0441\u044f<\/figcaption><\/figure>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u043c \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e &#171;\u043f\u0440\u043e\u043a\u0430\u0447\u0430\u0442\u044c&#187; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e Node.js-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412 <a href=\"\/ru\/post\/688182\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 &#171;\u044d\u043b\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0433\u043e&#187; \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430, \u0430 \u0432 \u044d\u0442\u043e\u0439 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#worker-threads\">Worker threads<\/a> \u0432 Node.js &#8212; \u043a\u0430\u043a\u0438\u0435 <strong>\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438<\/strong> \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0441 \u0436\u0434\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0430\u0445 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f <strong>\u0442\u0438\u043f\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b<\/strong>.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0438 \u043d\u0430\u043c \u0438\u0445 \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430 \u043a\u0430\u043a\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/SHA-2\">SHA-256<\/a>-\u0445\u044d\u0448 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: 4096 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 &#171;\u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f&#187; \u043e\u0431\u044a\u0435\u043c\u043e\u043c 64KB \u043a\u0430\u0436\u0434\u043e\u0435 (\u0432\u0441\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043b\u044e\u0431\u044f\u0442 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0435 \u0447\u0438\u0441\u043b\u0430) \u043f\u0440\u043e\u0445\u044d\u0448\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u044f\u043c\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435:<\/p>\n<pre><code class=\"javascript\">const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  const tsg = hrtime(); const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16)); console.log('generated:', Number(hrtime() - tsg) \/ 1e6 | 0, 'ms');  const tsh = hrtime(); const hashes = messages.map(data => createHash('sha256').update(data).digest('hex')); console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043d\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b &#171;\u0432 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u0435&#187;, \u0430 \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043e\u0442\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\u0445:<\/p>\n<pre><code>generated: 279 ms hashed:    914 ms<\/code><\/pre>\n<h2>\u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 &#171;<em>\u044f \u0441\u043b\u044b\u0448\u0430\u043b, \u0447\u0442\u043e CPU-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0442\u043e\u043a\u043e\u0432!<\/em>&#171;<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#worker-threads\">\u0446\u0438\u0442\u0430\u0442\u0430 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0447\u0443\u0442\u044c \u0438\u043d\u0430\u0447\u0435:<\/p>\n<blockquote>\n<p>Workers (threads) are useful for performing CPU-intensive JavaScript operations. They do not help much with I\/O-intensive work. The Node.js built-in asynchronous I\/O operations are more efficient than Workers can be.<\/p>\n<\/blockquote>\n<p>\u0412 \u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u0447\u0442\u043e <em>\u0434\u043b\u044f CPU-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 <\/em>\u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0442\u043e\u043a\u0438 <strong>\u043c\u043e\u0433\u0443\u0442<\/strong> \u043f\u043e\u043c\u043e\u0447\u044c (\u0430 \u043c\u043e\u0433\u0443\u0442 \u0432\u0435\u0434\u044c \u0438 \u043d\u0435 \u043f\u043e\u043c\u043e\u0447\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b), \u0430 <em>\u0434\u043b\u044f I\/O-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445<\/em> \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0430\u0436\u0435 \u0438 \u043d\u0435 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u041d\u043e \u0443 \u043d\u0430\u0441-\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 <em>CPU-intensive<\/em>, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f. \u0418 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u043e\u0433\u043e, \u0441\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441 <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#workerparentport\">\u043c\u0430\u043d\u0443\u0430\u043b\u0430<\/a>, \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441 \u043e\u0434\u043d\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  if (isMainThread) {   \/\/ \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a   const tsg = hrtime();   const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16));   console.log('generated:', Number(hrtime() - tsg) \/ 1e6 | 0, 'ms');    const hashes = messages.map(() => undefined); \/\/ \u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432   let remain = hashes.length; \/\/ \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432    let tsh;      const worker = new Worker(__filename); \/\/ \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430   worker     .on('online', () => {     \/\/ \u043f\u043e\u0442\u043e\u043a \u0433\u043e\u0442\u043e\u0432 \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443       tsh = hrtime();         \/\/ \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0437\u0430\u043c\u0435\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438       messages.forEach((data, id) => worker.postMessage({id, data})); \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c     })     .on('message', ({id, hash}) => {                                  \/\/ \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c       hashes[id] = hash;       if (!--remain) {        \/\/ \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043a\u043e\u043d\u0447\u0438\u043b\u0441\u044f, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b         console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');         worker.unref();       \/\/ \u043e\u0442\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f       }     }); } else {   \/\/ \u044d\u0442\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a   parentPort.on('message', ({id, data}) => {                          \/\/ \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c     parentPort.postMessage({                                          \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c       id     , hash : createHash('sha256').update(data).digest('hex')     });   }); }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0430\u043a-\u0442\u043e \u0443\u0432\u044f\u0437\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043d\u0435\u0435, \u0442\u043e <strong>\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u0432\u044f\u0437\u0443\u044e\u0449\u0435\u0433\u043e ID<\/strong> \u0442\u0443\u0434\u0430-\u043e\u0431\u0440\u0430\u0442\u043d\u043e &#8212; \u043d\u0430 \u0432\u0430\u0448\u0435\u0439 \u0441\u043e\u0432\u0435\u0441\u0442\u0438<\/p>\n<\/li>\n<li>\n<p>\u0432\u0441\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <strong>\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/strong>, \u043e\u0442\u043a\u0443\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e &#8230;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e <strong>\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 &#171;<em>\u0432\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e<\/em>&#171;<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a34\/68a\/c15\/a3468ac154943a2e5dd0a341c8b12527.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c\" width=\"757\" height=\"379\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a34\/68a\/c15\/a3468ac154943a2e5dd0a341c8b12527.png\"\/><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/figcaption><\/figure>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u0435\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0439 \u0441\u0445\u0435\u043c\u0435, \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0436\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c\u0441\u044f \u0437\u0430 \u0441\u0447\u0435\u0442 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043e\u043a. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434, \u0438&#8230; \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <strong>\u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043d\u0430 22.5%<\/strong>!<\/p>\n<pre><code>generated: 276 ms hashed:    1121 ms<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0430\u0441 \u043d\u0430\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442:<\/p>\n<ul>\n<li>\n<p><strong>\u0438\u0437\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u0430 \u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/strong> \u043c\u043e\u0433\u0443\u0442 \u0441\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u043d\u0435\u0442 \u0432\u0435\u0441\u044c \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h2>\n<p>\u041d\u043e \u043c\u044b \u0442\u0443\u0442 \u0432\u0441\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e <strong>\u043c\u043d\u043e\u0433\u043e<\/strong>\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043a-\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d! \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0430\u043c \u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u0430\u043b\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0435\u0435 \u0441\u0440\u0430\u0437\u0443 <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#workerworkerdata\">\u0447\u0435\u0440\u0435\u0437 workerData<\/a> &#8212; \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0436\u0438\u0434\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>'online'<\/code> \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>worker.postMessage<\/code>.<\/p>\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort , workerData \/\/ \u0434\u043b\u044f \u043f\u0440\u0438\u0435\u043c\u0430 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 } = require('node:worker_threads');  \/\/ ...  if (isMainThread) {   \/\/ ...      const tsh = hrtime();   messages.forEach((data, id) => {     const worker = new Worker(__filename, { \/\/ \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 - \u0441\u0432\u043e\u0439 worker       workerData : {id, data} \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435     });     worker       .on('message', ({id, hash}) => {         hashes[id] = hash;         if (!--remain) {           console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');         }       });   }); } else {   const {id, data} = workerData; \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u0430   parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   process.exit(); \/\/ \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a }<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4ed\/f13\/8c1\/4edf138c1030104607ff80f15bd2507b.png\" alt=\"\u041a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 - \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443\" title=\"\u041a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 - \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443\" width=\"700\" height=\"398\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4ed\/f13\/8c1\/4edf138c1030104607ff80f15bd2507b.png\"\/><figcaption>\u041a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 &#8212; \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443<\/figcaption><\/figure>\n<p>\u0423\u0440\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 <strong>\u0432\u0441\u0435 4 \u044f\u0434\u0440\u0430 CPU \u043d\u0430 100%<\/strong>! \u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e&#8230;<\/p>\n<pre><code>generated: 290 ms hashed:    95161 ms<\/code><\/pre>\n<p>\u0421\u043a\u043e\u043b\u044c\u043a\u043e-\u0441\u043a\u043e\u043b\u044c\u043a\u043e?!.. \u0414\u0430, \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b <strong>\u043f\u0440\u043e\u0438\u0433\u0440\u0430\u043b\u0438 \u0432 100 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0435-\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0437\u0430\u0442\u043e \u0443\u0437\u043d\u0430\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043d\u0446\u0438\u043f:<\/p>\n<ul>\n<li>\n<p><strong>\u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/strong> \u0436\u0435\u0441\u0442\u043a\u043e \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0443\u044e\u0442 \u0437\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c CPU, \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0430\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438) \u0438 \u0441\u043e\u043e\u0431\u0449\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u044c\u0448\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432, Round-robin<\/h2>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0434\u043d\u043e\u0433\u043e, \u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c &#171;\u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e&#187;. \u0418\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043d\u0435 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432\u0430\u043b\u0438 \u0437\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0438\u0445.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0440\u0430\u0437 \u0443 \u043d\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447 (\u0430 \u043d\u0435 \u0432\u0441\u0435 \u0438 \u043d\u0435 \u043e\u0434\u043d\u0430) &#8230;<\/p>\n<ul>\n<li>\n<p>\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c <strong>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447<\/strong> \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Round-robin_(%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC)\">Round-robin<\/a>, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e &#171;\u043f\u043e \u043a\u0440\u0443\u0433\u0443&#187;.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c0f\/93e\/584\/c0f93e58407c564dd6b32b8120fbe2c2.png\" alt=\"\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043f\u043e Round-robin\" title=\"\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043f\u043e Round-robin\" width=\"681\" height=\"511\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c0f\/93e\/584\/c0f93e58407c564dd6b32b8120fbe2c2.png\"\/><figcaption>\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043f\u043e Round-robin<\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0443 \u043d\u0430\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f CPU, \u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 <strong>\u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 CPU-\u044f\u0434\u0435\u0440<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0435 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u0441\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c:<\/p>\n<pre><code class=\"javascript\">\/\/ ...  if (isMainThread) {   \/\/ ...      let tsh;    const n = require('node:os').cpus().length; \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044f\u0434\u0435\u0440 CPU   Promise.all(     Array(n).fill().map(_ => { \/\/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c N \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 Promise       return new Promise((resolve, reject) => {         const worker = new Worker(__filename);         worker           .on('online', () => resolve(worker))           .on('message', ({id, hash}) => {             hashes[id] = hash;             if (!--remain) {               console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');               process.exit();             }           });       });         })   )     .then((workers) => { \/\/ \u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0433\u043e\u0442\u043e\u0432\u044b       const ln = workers.length;       tsh = hrtime();       messages.forEach((data, id) => {         const worker = workers[id % ln]; \/\/ \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \"\u043f\u043e \u043a\u0440\u0443\u0433\u0443\"         worker.postMessage({id, data});       });     }); } else {   parentPort.on('message', ({id, data}) => {     parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   }); }<\/code><\/pre>\n<pre><code>generated: 279 ms hashed:    375 ms<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0444\u0438\u0442, \u043f\u0440\u0438\u0447\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 &#8212; \u043d\u0430 4-\u044f\u0434\u0435\u0440\u043d\u043e\u043c CPU <strong>\u0432 2.5 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/strong>, \u0447\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f.<\/p>\n<h2>&#171;\u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0448\u0430\u0442\u044c \u0432 \u0433\u0440\u0430\u043c\u043c\u0430\u0445?&#187;<\/h2>\n<p>\u041d\u043e \u043a\u0442\u043e \u0441\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e &#8212; \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u043d\u0435 \u043c\u0435\u043d\u044c\u0448\u0435? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0447\u0443\u0442\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448 \u0442\u0435\u0441\u0442 \u0438 \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f \u0441\u0430\u043c\u0438, \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#performanceeventlooputilizationutilization1-utilization2\">worker.eventLoopUtilization<\/a>:<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0442\u0435\u0441\u0442\u0430 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  if (isMainThread) {   const tsg = hrtime();   const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16));   console.log('generated:', Number(hrtime() - tsg)\/1e6 | 0, 'ms');    const hashes = messages.map(() => undefined);   let remain;    const workers = []; \/\/ \u043f\u043e\u043b\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043f\u043e\u0442\u043e\u043a\u043e\u0432   let active = 1;     \/\/ \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u043a\u043e\u043b-\u0432\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432   let tsh;    process     .on('test:start', () => {       hashes.fill();       remain = hashes.length;        \/\/ \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u0442\u0435\u0441\u0442\u0430       workers.forEach(worker => worker.eLU = worker.performance.eventLoopUtilization());       tsh = hrtime();       messages.forEach((data, id) => {         const worker = workers[id % active]; \/\/ RR \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0440\u0435\u0434\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445         worker.postMessage({id, data});       });     })     .on('test:end', () => {       const duration = hrtime() - tsh;       \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043f\u043e \u0432\u0441\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c       workers.forEach(worker => worker.util = worker.performance.eventLoopUtilization(worker.eLU).utilization);       \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0441\u0440\u0435\u0434\u043d\u044e\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432       const avg = workers.slice(0, active).reduce((sum, worker) => sum + worker.util, 0) \/ active;        console.log(         'hashed ' + active.toString().padStart(2) + ':'       , (Number(duration)\/1e6 | 0).toString().padStart(4)       , 'ms | ' + (avg * 100 | 0) + ' | '       , workers.map(           worker => (worker.util * 100 | 0).toString().padStart(2) \/\/ % \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438         ).join(' ')       );        if (active &lt; n) {         active++;                   \/\/ \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043b-\u0432\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445         process.emit('test:start'); \/\/ \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442       }       else {         process.exit();       }     });    const n = 16;   Promise.all(     Array(n).fill().map(_ => {       return new Promise((resolve, reject) => {         const worker = new Worker(__filename);         worker           .on('online', () => resolve(worker))           .on('message', ({id, hash}) => {             hashes[id] = hash;             if (!--remain) {               process.emit('test:end'); \/\/ \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b             }           });       });         })   )     .then((result) => {       workers.push(...result);    \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0432 \u043e\u0431\u0449\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432       process.emit('test:start'); \/\/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442     }); } else {   parentPort.on('message', ({id, data}) => {     parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   }); }<\/code><\/pre>\n<pre><code>generated: 282 ms hashed  1: 1120 ms | 99 |  99  0  0  0  0  0  1  0  0  0  0  0  0  0  1  0 hashed  2:  609 ms | 97 |  95 99  0  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  3:  427 ms | 97 |  97 96 99  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  4:  381 ms | 94 |  93 90 95 97  0  0  0  0  0  0  0  0  0  0  0  0 hashed  5:  362 ms | 95 |  91 98 93 99 93  0  0  0  0  0  0  0  0  0  0  0 hashed  6:  328 ms | 95 |  96 92 93 96 97 92  0  0  0  0  0  0  0  0  0  0 hashed  7:  331 ms | 82 |  74 78 96 80 93 71 83  0  0  0  0  0  0  0  0  0 hashed  8:  319 ms | 90 |  91 89 89 83 98 85 94 92  0  0  0  0  0  0  0  0 hashed  9:  323 ms | 85 |  86 79 86 84 83 80 85 87 97  0  0  0  0  0  0  0 hashed 10:  312 ms | 84 |  94 84 77 83 89 84 86 81 81 78  0  0  0  0  0  0 hashed 11:  313 ms | 83 |  94 75 85 94 88 80 82 77 81 82 72  0  0  0  0  0 hashed 12:  323 ms | 70 |  78 73 65 91 62 59 77 77 77 77 53 51  0  0  0  0 hashed 13:  307 ms | 68 |  77 50 46 99 64 58 60 77 74 73 56 75 80  0  0  0 hashed 14:  309 ms | 65 |  94 77 73 67 58 70 30 85 25 80 68 89 71 25  0  0 hashed 15:  304 ms | 56 |  58 89 82 23 79 79 75 22 23 25 79 46 71 53 34  0 hashed 16:  358 ms | 51 |  25 40 26 54 61 39 59 60 68 69 20 44 69 46 59 74<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/faf\/7a0\/a7b\/faf7a0a7b32daedbb36a28928ec86bec.png\" alt=\"\u0414\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0435\u0441\u0442\u0430 \u0438 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\" title=\"\u0414\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0435\u0441\u0442\u0430 \u0438 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/faf\/7a0\/a7b\/faf7a0a7b32daedbb36a28928ec86bec.png\"\/><figcaption>\u0414\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0435\u0441\u0442\u0430 \u0438 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/figcaption><\/figure>\n<p>\u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u0441\u0442\u044c: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u043e \u043f\u0440\u0438 15 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u0430 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043f\u0440\u0438 4, \u0447\u0442\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u043e \u0431\u044b \u043f\u043e\u043b\u043d\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u044f\u0434\u0435\u0440 CPU. \u041f\u0440\u0430\u0432\u0434\u0430, \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u044d\u0442\u0438\u0445 15 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0431\u044b\u043b\u0430 <strong>\u043d\u0438\u0436\u0435 60%<\/strong>, \u043f\u0440\u0438\u0447\u0435\u043c \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 &#8212; 22%, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 &#8212; 89%, \u0430 \u0432\u0435\u0434\u044c \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e&#8230;<\/p>\n<ul>\n<li>\n<p>\u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 <strong>\u043f\u043e\u0442\u043e\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u0430\u0438\u0432\u0430\u044e\u0442<\/strong>, \u0435\u0441\u043b\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u0438\u043c \u0437\u0430\u0434\u0430\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0438\u043b\u0438 \u043e\u043d \u043e\u0442\u0434\u0430\u043b \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u043e\u043c\u0443, \u043a\u0442\u043e \u0431\u044b\u043b \u0441\u0432\u043e\u0431\u043e\u0434\u0435\u043d \u0438 \u0441\u043c\u043e\u0433 \u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0441\u0440\u0430\u0437\u0443, \u0430 \u0434\u0440\u0443\u0433\u043e\u043c\u0443 <strong>\u043f\u043e\u0442\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0449\u0435 \u0431\u044b\u043b \u0437\u0430\u043d\u044f\u0442<\/strong> \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u0437\u0430\u0434\u0430\u043d\u0438\u0435\u043c<\/p>\n<\/li>\n<\/ul>\n<p>\u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c\u0438 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e\u0441\u0442\u044f\u043c\u0438 &#8212; \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/689144\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/689144\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption>\u041c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u0433\u0438\u0434\u0440\u0430 \u0431\u043e\u043b\u044c\u043d\u043e \u043a\u0443\u0441\u0430\u0435\u0442\u0441\u044f<\/figcaption><\/figure>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u043c \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e &#171;\u043f\u0440\u043e\u043a\u0430\u0447\u0430\u0442\u044c&#187; \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e Node.js-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412 <a href=\"\/ru\/post\/688182\/\">\u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 &#171;\u044d\u043b\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0433\u043e&#187; \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430, \u0430 \u0432 \u044d\u0442\u043e\u0439 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#worker-threads\">Worker threads<\/a> \u0432 Node.js &#8212; \u043a\u0430\u043a\u0438\u0435 <strong>\u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438<\/strong> \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0441 \u0436\u0434\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0430\u0445 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0438 \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044f <strong>\u0442\u0438\u043f\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b<\/strong>.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0438 \u043d\u0430\u043c \u0438\u0445 \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e, \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0437\u0430 \u043a\u0430\u043a\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/SHA-2\">SHA-256<\/a>-\u0445\u044d\u0448 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: 4096 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 &#171;\u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f&#187; \u043e\u0431\u044a\u0435\u043c\u043e\u043c 64KB \u043a\u0430\u0436\u0434\u043e\u0435 (\u0432\u0441\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u043b\u044e\u0431\u044f\u0442 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0435 \u0447\u0438\u0441\u043b\u0430) \u043f\u0440\u043e\u0445\u044d\u0448\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u044f\u043c\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435:<\/p>\n<pre><code class=\"javascript\">const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  const tsg = hrtime(); const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16)); console.log('generated:', Number(hrtime() - tsg) \/ 1e6 | 0, 'ms');  const tsh = hrtime(); const hashes = messages.map(data => createHash('sha256').update(data).digest('hex')); console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043d\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b &#171;\u0432 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u0435&#187;, \u0430 \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043e\u0442\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\u0445:<\/p>\n<pre><code>generated: 279 ms hashed:    914 ms<\/code><\/pre>\n<h2>\u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 &#171;<em>\u044f \u0441\u043b\u044b\u0448\u0430\u043b, \u0447\u0442\u043e CPU-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u0442\u043e\u043a\u043e\u0432!<\/em>&#171;<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#worker-threads\">\u0446\u0438\u0442\u0430\u0442\u0430 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0447\u0443\u0442\u044c \u0438\u043d\u0430\u0447\u0435:<\/p>\n<blockquote>\n<p>Workers (threads) are useful for performing CPU-intensive JavaScript operations. They do not help much with I\/O-intensive work. The Node.js built-in asynchronous I\/O operations are more efficient than Workers can be.<\/p>\n<\/blockquote>\n<p>\u0412 \u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u0447\u0442\u043e <em>\u0434\u043b\u044f CPU-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 <\/em>\u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0442\u043e\u043a\u0438 <strong>\u043c\u043e\u0433\u0443\u0442<\/strong> \u043f\u043e\u043c\u043e\u0447\u044c (\u0430 \u043c\u043e\u0433\u0443\u0442 \u0432\u0435\u0434\u044c \u0438 \u043d\u0435 \u043f\u043e\u043c\u043e\u0447\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b), \u0430 <em>\u0434\u043b\u044f I\/O-\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445<\/em> \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0430\u0436\u0435 \u0438 \u043d\u0435 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u041d\u043e \u0443 \u043d\u0430\u0441-\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0432 \u0447\u0438\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435 <em>CPU-intensive<\/em>, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f. \u0418 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u043e\u0433\u043e, \u0441\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441 <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#workerparentport\">\u043c\u0430\u043d\u0443\u0430\u043b\u0430<\/a>, \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441 \u043e\u0434\u043d\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  if (isMainThread) {   \/\/ \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a   const tsg = hrtime();   const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16));   console.log('generated:', Number(hrtime() - tsg) \/ 1e6 | 0, 'ms');    const hashes = messages.map(() => undefined); \/\/ \u0433\u043e\u0442\u043e\u0432\u0438\u043c \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432   let remain = hashes.length; \/\/ \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432    let tsh;      const worker = new Worker(__filename); \/\/ \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430   worker     .on('online', () => {     \/\/ \u043f\u043e\u0442\u043e\u043a \u0433\u043e\u0442\u043e\u0432 \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443       tsh = hrtime();         \/\/ \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0437\u0430\u043c\u0435\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438       messages.forEach((data, id) => worker.postMessage({id, data})); \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c     })     .on('message', ({id, hash}) => {                                  \/\/ \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c       hashes[id] = hash;       if (!--remain) {        \/\/ \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043a\u043e\u043d\u0447\u0438\u043b\u0441\u044f, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u044b \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b         console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');         worker.unref();       \/\/ \u043e\u0442\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c\u0441\u044f       }     }); } else {   \/\/ \u044d\u0442\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a   parentPort.on('message', ({id, data}) => {                          \/\/ \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c     parentPort.postMessage({                                          \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c       id     , hash : createHash('sha256').update(data).digest('hex')     });   }); }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0430\u043a-\u0442\u043e \u0443\u0432\u044f\u0437\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043d\u0435\u0435, \u0442\u043e <strong>\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u0432\u044f\u0437\u0443\u044e\u0449\u0435\u0433\u043e ID<\/strong> \u0442\u0443\u0434\u0430-\u043e\u0431\u0440\u0430\u0442\u043d\u043e &#8212; \u043d\u0430 \u0432\u0430\u0448\u0435\u0439 \u0441\u043e\u0432\u0435\u0441\u0442\u0438<\/p>\n<\/li>\n<li>\n<p>\u0432\u0441\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <strong>\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/strong>, \u043e\u0442\u043a\u0443\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e &#8230;<\/p>\n<\/li>\n<li>\n<p>\u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e <strong>\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u0432\u0435\u0442\u043e\u0432<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 &#171;<em>\u0432\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e<\/em>&#171;<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435:<\/p>\n<figure class=\"full-width\"><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/figcaption><\/figure>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u0435\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0439 \u0441\u0445\u0435\u043c\u0435, \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0436\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c\u0441\u044f \u0437\u0430 \u0441\u0447\u0435\u0442 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043e\u043a. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u043a\u043e\u0434, \u0438&#8230; \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <strong>\u0432\u044b\u0440\u043e\u0441\u043b\u043e \u043d\u0430 22.5%<\/strong>!<\/p>\n<pre><code>generated: 276 ms hashed:    1121 ms<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0430\u0441 \u043d\u0430\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442:<\/p>\n<ul>\n<li>\n<p><strong>\u0438\u0437\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u0430 \u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u043c<\/strong> \u043c\u043e\u0433\u0443\u0442 \u0441\u0432\u0435\u0441\u0442\u0438 \u043d\u0430 \u043d\u0435\u0442 \u0432\u0435\u0441\u044c \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<h2>\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h2>\n<p>\u041d\u043e \u043c\u044b \u0442\u0443\u0442 \u0432\u0441\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e <strong>\u043c\u043d\u043e\u0433\u043e<\/strong>\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043a-\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d! \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0430\u043c \u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u0430\u043b\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0435\u0435 \u0441\u0440\u0430\u0437\u0443 <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#workerworkerdata\">\u0447\u0435\u0440\u0435\u0437 workerData<\/a> &#8212; \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u043c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0436\u0438\u0434\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>'online'<\/code> \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>worker.postMessage<\/code>.<\/p>\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort , workerData \/\/ \u0434\u043b\u044f \u043f\u0440\u0438\u0435\u043c\u0430 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 } = require('node:worker_threads');  \/\/ ...  if (isMainThread) {   \/\/ ...      const tsh = hrtime();   messages.forEach((data, id) => {     const worker = new Worker(__filename, { \/\/ \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 - \u0441\u0432\u043e\u0439 worker       workerData : {id, data} \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435     });     worker       .on('message', ({id, hash}) => {         hashes[id] = hash;         if (!--remain) {           console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');         }       });   }); } else {   const {id, data} = workerData; \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u043e\u0442\u043e\u043a\u0430   parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   process.exit(); \/\/ \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a }<\/code><\/pre>\n<figure class=\"full-width\"><figcaption>\u041a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 &#8212; \u043f\u043e \u043f\u043e\u0442\u043e\u043a\u0443<\/figcaption><\/figure>\n<p>\u0423\u0440\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 <strong>\u0432\u0441\u0435 4 \u044f\u0434\u0440\u0430 CPU \u043d\u0430 100%<\/strong>! \u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e&#8230;<\/p>\n<pre><code>generated: 290 ms hashed:    95161 ms<\/code><\/pre>\n<p>\u0421\u043a\u043e\u043b\u044c\u043a\u043e-\u0441\u043a\u043e\u043b\u044c\u043a\u043e?!.. \u0414\u0430, \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b <strong>\u043f\u0440\u043e\u0438\u0433\u0440\u0430\u043b\u0438 \u0432 100 \u0440\u0430\u0437<\/strong> \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0435-\u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0437\u0430\u0442\u043e \u0443\u0437\u043d\u0430\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043d\u0446\u0438\u043f:<\/p>\n<ul>\n<li>\n<p><strong>\u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/strong> \u0436\u0435\u0441\u0442\u043a\u043e \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0443\u044e\u0442 \u0437\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b (\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c CPU, \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0430\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438) \u0438 \u0441\u043e\u043e\u0431\u0449\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u044c\u0448\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432, Round-robin<\/h2>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0434\u043d\u043e\u0433\u043e, \u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c &#171;\u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e&#187;. \u0418\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043d\u0435 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432\u0430\u043b\u0438 \u0437\u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0438\u0445.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0440\u0430\u0437 \u0443 \u043d\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447 (\u0430 \u043d\u0435 \u0432\u0441\u0435 \u0438 \u043d\u0435 \u043e\u0434\u043d\u0430) &#8230;<\/p>\n<ul>\n<li>\n<p>\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c <strong>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447<\/strong> \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438<\/p>\n<\/li>\n<\/ul>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Round-robin_(%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC)\">Round-robin<\/a>, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0430\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e &#171;\u043f\u043e \u043a\u0440\u0443\u0433\u0443&#187;.<\/p>\n<figure class=\"full-width\"><figcaption>\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u043f\u043e Round-robin<\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0443 \u043d\u0430\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f CPU, \u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 <strong>\u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443 \u043d\u0430\u0441 CPU-\u044f\u0434\u0435\u0440<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0435 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432\u0441\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c:<\/p>\n<pre><code class=\"javascript\">\/\/ ...  if (isMainThread) {   \/\/ ...      let tsh;    const n = require('node:os').cpus().length; \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044f\u0434\u0435\u0440 CPU   Promise.all(     Array(n).fill().map(_ => { \/\/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c N \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 Promise       return new Promise((resolve, reject) => {         const worker = new Worker(__filename);         worker           .on('online', () => resolve(worker))           .on('message', ({id, hash}) => {             hashes[id] = hash;             if (!--remain) {               console.log('hashed:   ', Number(hrtime() - tsh) \/ 1e6 | 0, 'ms');               process.exit();             }           });       });         })   )     .then((workers) => { \/\/ \u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0433\u043e\u0442\u043e\u0432\u044b       const ln = workers.length;       tsh = hrtime();       messages.forEach((data, id) => {         const worker = workers[id % ln]; \/\/ \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0438 \"\u043f\u043e \u043a\u0440\u0443\u0433\u0443\"         worker.postMessage({id, data});       });     }); } else {   parentPort.on('message', ({id, data}) => {     parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   }); }<\/code><\/pre>\n<pre><code>generated: 279 ms hashed:    375 ms<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0444\u0438\u0442, \u043f\u0440\u0438\u0447\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 &#8212; \u043d\u0430 4-\u044f\u0434\u0435\u0440\u043d\u043e\u043c CPU <strong>\u0432 2.5 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/strong>, \u0447\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f.<\/p>\n<h2>&#171;\u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0448\u0430\u0442\u044c \u0432 \u0433\u0440\u0430\u043c\u043c\u0430\u0445?&#187;<\/h2>\n<p>\u041d\u043e \u043a\u0442\u043e \u0441\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e &#8212; \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u043d\u0435 \u043c\u0435\u043d\u044c\u0448\u0435? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0447\u0443\u0442\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448 \u0442\u0435\u0441\u0442 \u0438 \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f \u0441\u0430\u043c\u0438, \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nodejs.org\/dist\/latest-v16.x\/docs\/api\/worker_threads.html#performanceeventlooputilizationutilization1-utilization2\">worker.eventLoopUtilization<\/a>:<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0442\u0435\u0441\u0442\u0430 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  if (isMainThread) {   const tsg = hrtime();   const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(1 &lt;&lt; 16));   console.log('generated:', Number(hrtime() - tsg)\/1e6 | 0, 'ms');    const hashes = messages.map(() => undefined);   let remain;    const workers = []; \/\/ \u043f\u043e\u043b\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043f\u043e\u0442\u043e\u043a\u043e\u0432   let active = 1;     \/\/ \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u043a\u043e\u043b-\u0432\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432   let tsh;    process     .on('test:start', () => {       hashes.fill();       remain = hashes.length;        \/\/ \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u0442\u0435\u0441\u0442\u0430       workers.forEach(worker => worker.eLU = worker.performance.eventLoopUtilization());       tsh = hrtime();       messages.forEach((data, id) => {         const worker = workers[id % active]; \/\/ RR \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0440\u0435\u0434\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445         worker.postMessage({id, data});       });     })     .on('test:end', () => {       const duration = hrtime() - tsh;       \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043f\u043e \u0432\u0441\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c       workers.forEach(worker => worker.util =<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-338564","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338564"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338564\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}