{"id":338998,"date":"2022-09-29T09:00:34","date_gmt":"2022-09-29T09:00:34","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338998"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338998","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 3: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c, \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438)<\/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<p>\u0412 <a href=\"\/ru\/post\/689406\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c \u043d\u0430 \u043c\u044b\u0441\u043b\u0438, \u0447\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 <strong>\u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u0441\u0430\u043c\u0438\u0445 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong>, \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c, \u043f\u043e\u043a\u0430 \u0438\u0445 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442 \u043a\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c5d\/f65\/52a\/c5df6552a2534f2f05baa72835c976f3.jpeg\" alt=\"Threads \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\" title=\"Threads \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\" width=\"1829\" height=\"1280\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c5d\/f65\/52a\/c5df6552a2534f2f05baa72835c976f3.jpeg\" data-blurred=\"true\"\/><figcaption>Threads \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438<\/figcaption><\/figure>\n<p>\u041d\u043e \u0442\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0434\u0432\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<ul>\n<li>\n<p>\u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a;<\/p>\n<\/li>\n<li>\n<p>\u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u043d\u043e \u0438 \u0434\u0432\u0430\u0436\u0434\u044b \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0434\u0432\u0430 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e: <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%B4%D0%B5%D0%BB%D1%8F%D0%B5%D0%BC%D0%B0%D1%8F_%D0%BF%D0%B0%D0%BC%D1%8F%D1%82%D1%8C\">\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f (shared) \u043f\u0430\u043c\u044f\u0442\u044c<\/a> \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 (thread-safe, Atomics) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043d\u0435\u0439.<\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/h2>\n<p>\u0412\u0441\u0435 \u043d\u0430\u0448\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u043c\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0431\u0430\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9C%D0%B5%D0%B6%D0%BF%D1%80%D0%BE%D1%86%D0%B5%D1%81%D1%81%D0%BD%D0%BE%D0%B5_%D0%B2%D0%B7%D0%B0%D0%B8%D0%BC%D0%BE%D0%B4%D0%B5%D0%B9%D1%81%D1%82%D0%B2%D0%B8%D0%B5\">IPC<\/a>. \u041d\u043e \u0432 \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0437\u0430\u0432\u0435\u0434\u043e\u043c\u043e \u0442\u0435\u0440\u044f\u0435\u043c \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u044b &#171;\u0437\u0432\u0430\u0442\u044c&#187; \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0447\u0442\u043e-\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c. \u0410 \u043d\u0435\u043b\u044c\u0437\u044f \u043b\u0438 \u043a\u0430\u043a-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0431\u0435\u0437 \u0432\u043e\u0442 \u044d\u0442\u043e\u0439 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0439 \u043e\u0431\u0432\u044f\u0437\u043a\u0438?<\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%B4%D0%B5%D0%BB%D1%8F%D0%B5%D0%BC%D0%B0%D1%8F_%D0%BF%D0%B0%D0%BC%D1%8F%D1%82%D1%8C\">\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438<\/a>, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043f\u043e\u0434 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u0445. \u0412 JavaScript \u0437\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/SharedArrayBuffer\">SharedArrayBuffer<\/a>.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7d6\/14e\/1d3\/7d614e1d361e9842e5dffcbda20307b3.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 JavaScript\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 JavaScript\" width=\"228\" height=\"322\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7d6\/14e\/1d3\/7d614e1d361e9842e5dffcbda20307b3.png\"\/><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 JavaScript<\/figcaption><\/figure>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 &#171;\u0437\u0430\u0434\u0430\u0447\u0438&#187; \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u044b, \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e <code>4K x 64KB = 256MB<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0432\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 <strong>\u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c &#171;\u043d\u043e\u043c\u0435\u0440&#187; \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong>. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u0435\u043b\u0430\u0435\u0442 <code>n++<\/code>, \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0435 \u0438 \u0438\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 <code>#n<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/93f\/430\/f60\/93f430f60b48674427c8ccd8a4306f3d.png\" alt=\"\u041e\u0431\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 &quot;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439&quot; \u0437\u0430\u0434\u0430\u0447\u0438\" title=\"\u041e\u0431\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 &quot;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439&quot; \u0437\u0430\u0434\u0430\u0447\u0438\" width=\"757\" height=\"303\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/93f\/430\/f60\/93f430f60b48674427c8ccd8a4306f3d.png\"\/><figcaption>\u041e\u0431\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 &#171;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439&#187; \u0437\u0430\u0434\u0430\u0447\u0438<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u044b <code>SharedArrayBuffer<\/code>-\u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0438\u0445 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ ... const tasksCount = 1 &lt;&lt; 12; const taskSize   = 1 &lt;&lt; 16;  if (isMainThread) { \/\/ ...   const sharedBufferData = new SharedArrayBuffer(tasksCount * taskSize);   const sharedBufferNext = new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT);   const sharedData = new Uint8Array(sharedBufferData);   const sharedNext = new Uint32Array(sharedBufferNext); \/\/ ...       const worker = new Worker(__filename,         {           workerData : {             data : sharedBufferData           , next : sharedBufferNext           }         }       );   \/\/ ... } else {   const {data, next} = workerData;   const sharedData = new Uint8Array(data);   const sharedNext = new Uint32Array(next); \/\/ ... }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0435\u043c\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430, \u043c\u044b <strong>\u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432<\/strong> <code>transferList<\/code> \u0434\u043b\u044f \u043d\u0435-\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0438 \u0442\u0430\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h2>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u043e\u0447\u043d\u0435\u0435, \u0441 \u0435\u0433\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u044b &#171;\u043b\u044e\u0431\u044f\u0442&#187; \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0435, \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u0435 <strong>\u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/strong>, \u0442\u043e \u043d\u0430\u0448 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 <code>load<\/code> &#8212; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440, <code>inc<\/code> &#8212; \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430 \u0438 <code>store<\/code> &#8212; \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0433\u0440\u0443\u0431\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0441\u0445\u0435\u043c\u043e\u0439:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/66c\/67d\/570\/66c67d57012d8a7fee0d44863a163fdb.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438\" width=\"265\" height=\"455\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/66c\/67d\/570\/66c67d57012d8a7fee0d44863a163fdb.png\"\/><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/figcaption><\/figure>\n<p>\u041d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043c\u044b \u0443\u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u0432 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 &#8212; \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <strong>\u043d\u0435\u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/strong>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cf8\/515\/954\/cf8515954adedd2bacb863af7eb8126d.png\" alt=\"\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0432\u0443\u0445 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 2... \u0438\u043b\u0438 \u043d\u0430 1\" title=\"\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0432\u0443\u0445 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 2... \u0438\u043b\u0438 \u043d\u0430 1\" width=\"908\" height=\"757\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cf8\/515\/954\/cf8515954adedd2bacb863af7eb8126d.png\"\/><figcaption>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0432\u0443\u0445 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 2&#8230; \u0438\u043b\u0438 \u043d\u0430 1<\/figcaption><\/figure>\n<p>\u0422\u0430\u043a \u0432\u043e\u0442, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0435\u0434\u043e\u043c\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0438 \u0431\u044b\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\">Atomics<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <strong>\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/strong> \u043d\u0430\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u044f\u0447\u0435\u0439\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u043b\u043e\u0436\u0438\u0442\u044c <strong>\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438<\/strong>: \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c &#8212; \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0438 \u0441\u043d\u0438\u043c\u0430\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043d\u0435 \u0432\u044b\u0448\u043b\u043e &#8212; \u0436\u0434\u0435\u043c, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430\u043b\u043e\u0436\u0438\u0442\u044c:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/76d\/1fa\/594\/76d1fa594aea73078470bdfe6b56d648.png\" alt=\"\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 Atomics-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445\" title=\"\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 Atomics-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445\" width=\"492\" height=\"757\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/76d\/1fa\/594\/76d1fa594aea73078470bdfe6b56d648.png\"\/><figcaption>\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 Atomics-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445<\/figcaption><\/figure>\n<p>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\">Atomics<\/a> \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u043e-\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435<\/strong>: <code>add, sub, and, or, xor<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u043e\u0431\u043c\u0435\u043d\u043d\u044b\u0435<\/strong>: <code>exchange, compareExchange<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0447\u0442\u0435\u043d\u0438\u044f-\u0437\u0430\u043f\u0438\u0441\u0438<\/strong>: <code>load, store<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438<\/strong>: <code>isLockFree, notify, wait, waitAsync<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u044b\u0445 \u0442\u0440\u0435\u0445 \u0433\u0440\u0443\u043f\u043f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0430 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e, \u0445\u043e\u0440\u043e\u0448\u043e <a href=\"https:\/\/v8.dev\/features\/atomics\">\u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0431\u043b\u043e\u0433\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 V8<\/a>.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u043d\u0435 &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442&#187;, \u0430 <code>Atomics.add<\/code>:<\/p>\n<pre><code class=\"javascript\">  parentPort.on('message', () => {     do {       const id = Atomics.add(sharedNext, 0, 1); \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442       if (id >= tasksCount) { \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         break;       }       processMessage({id, data : sharedData.subarray(id * taskSize, (id + 1) * taskSize)});     }     while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0441\u0442\u044c   });<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0431\u0449\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043b\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/subarray\">.subarray()<\/a>, \u0430 \u043d\u0435 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/slice\">.slice()<\/a>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043d\u0442, \u0430 \u043f\u0435\u0440\u0432\u044b\u0439 &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0442\u043e\u0442 \u0436\u0435 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 <strong>\u043f\u043e\u0442\u043e\u043a\u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0436\u0434\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043f\u0440\u0438 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/strong> (\u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0441\u0435\u0440\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u044b), \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u0438\u0445 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/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\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort , workerData } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  const tasksCount = 1 &lt;&lt; 12; const taskSize   = 1 &lt;&lt; 16;  if (isMainThread) {   const tsg = hrtime();   const messages = Array(tasksCount).fill().map(_ => randomBytes(taskSize));   console.log('generated:', Number(hrtime() - tsg)\/1e6 | 0, 'ms');    const hashes = messages.map(() => undefined);   let remain;    const workers = [];   let active = 1;   let tsh;    \/\/ \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438   const sharedBufferData = new SharedArrayBuffer(tasksCount * taskSize);   const sharedBufferNext = new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT);   const sharedData = new Uint8Array(sharedBufferData);   const sharedNext = new Uint32Array(sharedBufferNext);    process     .on('test:start', () => {       hashes.fill();       remain = hashes.length;       \/\/ \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u043e\u0431\u0449\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0437\u0430\u0434\u0430\u0447       sharedData.fill(0);       Atomics.store(sharedNext, 0, 0);        workers.forEach(worker => worker.eLU = worker.performance.eventLoopUtilization());       tsh = hrtime();       \/\/ \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0435\u043b\u0430\u043c\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439       messages.forEach((data, id) => {         sharedData.set(data, id * taskSize);       });       \/\/ \"\u0434\u0435\u0440\u0433\u0430\u0435\u043c\" \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438       workers.slice(0, active).forEach(worker => worker.postMessage(undefined));     })     .on('test:end', () => {       const duration = hrtime() - tsh;       workers.forEach(worker => worker.util = worker.performance.eventLoopUtilization(worker.eLU).utilization);       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)         ).join(' ')       );        if (active &lt; n) {         active++;         process.emit('test:start');       }       else {         process.exit();       }     });    const n = 16;   Promise.all(     Array(n).fill().map(_ => new Promise((resolve, reject) => {       \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043f\u043e\u0442\u043e\u043a \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438       const worker = new Worker(__filename,         {           workerData : {             data : sharedBufferData           , next : sharedBufferNext           }         }       );       worker         .on('online', () => resolve(worker))         .on('message', ({id, hash}) => {           hashes[id] = hash;           if (!--remain) {             process.emit('test:end');           }         });     }))   )     .then((result) => {       workers.push(...result);       process.emit('test:start');     }); } else {   \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c TypedArray \u043d\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0445 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0445   const {data, next} = workerData;   const sharedData = new Uint8Array(data);   const sharedNext = new Uint32Array(next);      const processMessage = ({id, data}) => parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   parentPort.on('message', () => {     do {       const id = Atomics.add(sharedNext, 0, 1); \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442       if (id >= tasksCount) { \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         break;       }       processMessage({id, data : sharedData.subarray(id * taskSize, (id + 1) * taskSize)}); \/\/ \u043d\u0435 .slice()!     }     while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0441\u0442\u044c   }); }<\/code><\/pre>\n<pre><code>generated: 278 ms hashed  1:  996 ms | 95 |  95  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  2:  523 ms | 91 |  91 91  0  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  3:  374 ms | 86 |  86 86 86  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  4:  297 ms | 82 |  82 82 82 82  0  0  0  0  0  0  0  0  0  0  0  0 hashed  5:  308 ms | 81 |  83 83 83 83 73  0  0  0  0  0  0  0  0  0  0  0 hashed  6:  287 ms | 79 |  82 82 82 82 71 71  0  0  0  0  0  0  0  0  0  0 hashed  7:  291 ms | 77 |  82 82 82 82 71 71 71  0  0  0  0  0  0  0  0  0 hashed  8:  290 ms | 77 |  83 83 83 83 72 72 71 71  0  0  0  0  0  0  0  0 hashed  9:  295 ms | 76 |  83 83 83 83 72 72 72 72 62  0  0  0  0  0  0  0 hashed 10:  299 ms | 73 |  82 82 82 80 72 72 72 68 61 61  0  0  0  0  0  0 hashed 11:  290 ms | 72 |  82 82 82 82 71 71 70 70 60 60 59  0  0  0  0  0 hashed 12:  307 ms | 71 |  82 82 82 81 71 71 71 71 61 60 60 60  0  0  0  0 hashed 13:  296 ms | 67 |  80 80 80 80 69 69 69 67 59 58 58 57 48  0  0  0 hashed 14:  301 ms | 67 |  82 81 81 81 70 70 70 70 60 60 59 59 49 49  0  0 hashed 15:  299 ms | 67 |  82 82 82 82 72 71 71 71 61 61 61 61 50 49 50  0 hashed 16:  296 ms | 63 |  80 80 80 80 69 69 69 69 58 58 58 57 47 47 46 45<\/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\/4c8\/50e\/fcd\/4c850efcd1186ea05b7d5711a0b09e52.png\" alt=\"Atomics-\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c\" title=\"Atomics-\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4c8\/50e\/fcd\/4c850efcd1186ea05b7d5711a0b09e52.png\"\/><figcaption>Atomics-\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c<\/figcaption><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043d\u043e \u0438 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u0438 4 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, <strong>\u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044f\u0434\u0435\u0440 CPU<\/strong>. \u0410 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0438\u0445 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0443\u0436\u0435 \u043d\u0435 \u0434\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430, \u043a\u0440\u043e\u043c\u0435 <strong>\u0440\u043e\u0441\u0442\u0430 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438<\/strong>, \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u043e\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0436\u0434\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u043e\u043b\u044c\u0448\u0435 \u0438 \u0434\u043e\u043b\u044c\u0448\u0435.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0433\u0440\u0430\u0444\u0438\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0442\u0430\u043d\u0435\u0442 \u0435\u0449\u0435 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435. \u0414\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u044f\u0434\u0435\u0440 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u0430 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0430\u0434\u0430\u0435\u0442, \u0430 \u043f\u043e\u0442\u043e\u043c, \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043e\u0431\u0449\u0435\u0435 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u0430 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 (<code>duration * cpuN * cpuAvg%<\/code>) \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0441\u0442\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/492\/ba4\/38c\/492ba438cc6053122cb16b4a8c609d0c.png\" alt=\"\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, 4 \u044f\u0434\u0440\u0430\" title=\"\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, 4 \u044f\u0434\u0440\u0430\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/492\/ba4\/38c\/492ba438cc6053122cb16b4a8c609d0c.png\"\/><figcaption>\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, 4 \u044f\u0434\u0440\u0430<\/figcaption><\/figure>\n<details class=\"spoiler\">\n<summary>\u041f\u0440\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5ba\/931\/fc3\/5ba931fc3a49245c8f09dab6e2dda0f6.png\" alt=\"\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445\" title=\"\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445\" width=\"1203\" height=\"596\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5ba\/931\/fc3\/5ba931fc3a49245c8f09dab6e2dda0f6.png\"\/><figcaption>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445<\/figcaption><\/figure>\n<p>\u041f\u043e \u0433\u0440\u0430\u0444\u0438\u043a\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0435 4 \u043f\u043e\u0442\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 <strong>\u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430 80-85%<\/strong>, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 &#8212; 70-75%, \u0434\u0430\u043b\u044c\u0448\u0435 55-60% \u0438 \u043f\u043e\u0442\u043e\u043c \u0443\u0436\u0435 \u0442\u043e\u043b\u044c\u043a\u043e 45-50%. \u0422\u043e \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0434\u043e\u043b\u044e \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0432\u044b\u0435 4 \u043f\u043e\u0442\u043e\u043a\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044f\u0434\u0440\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u0430 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0436\u0434\u0430\u0442\u044c.<\/p>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u041f\u0440\u043e \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438: .subarray() vs .slice()<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0412\u044b\u0448\u0435 \u044f \u0430\u043a\u0446\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>.subarray()<\/code>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0446\u0435\u043d\u0438\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0440\u043e\u0433\u043e.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8d7\/f9b\/368\/8d7f9b368bc40b4da1aa2e3e5720807e.png\" alt=\".slice \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c, .subarray - \u043d\u0435\u0442\" title=\".slice \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c, .subarray - \u043d\u0435\u0442\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8d7\/f9b\/368\/8d7f9b368bc40b4da1aa2e3e5720807e.png\"\/><figcaption>.slice \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c, .subarray &#8212; \u043d\u0435\u0442<\/figcaption><\/figure>\n<p>\u041f\u0440\u0438 4 \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u043d\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u043d\u0443\u0442\u0440\u0438<code>.slice<\/code> \u043c\u044b <strong>\u0442\u0435\u0440\u044f\u0435\u043c \u0434\u043e 35% \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>!<\/p>\n<\/div>\n<\/details>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/h2>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0443 \u043d\u0430\u0441 \u0442\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e &#171;\u043d\u0435\u0447\u0435\u0441\u0442\u043d\u044b\u0439&#187; \u0442\u0435\u0441\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 (\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0435\u0435 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043d\u043e\u0433\u043e-\u043c\u043d\u043e\u0433\u043e). \u0412 \u0440\u0430\u0431\u043e\u0442\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c &#8212; \u043a\u043e\u0433\u0434\u0430 <strong>\u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 &#171;\u0432\u043b\u0430\u0434\u0435\u0435\u0442&#187; \u0441\u0432\u043e\u0438\u043c<\/strong> \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c, \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u0443 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0435\u0442 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0434\u043b\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043d\u0430 \u043e\u0431\u0449\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0435 &#8212; \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043d\u0438\u0445 \u043d\u0435\u0442 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e. \u0422\u0430\u043a \u0434\u0430\u0432\u0430\u0439\u0442\u0435 <strong>\u0437\u0430\u0441\u0442\u0430\u0432\u0438\u043c \u043f\u043e\u0442\u043e\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u0436\u0434\u0430\u0442\u044c<\/strong>, \u043f\u043e\u043a\u0430 \u0435\u043c\u0443 \u043d\u0435 \u0434\u0430\u0434\u0443\u0442 \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0443 &#8212; \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\/wait\">Atomics.wait()<\/a>:<\/p>\n<pre><code class=\"javascript\">const THREAD_FREE = -1; \/\/ ... \/\/ \u043a\u043e\u0434 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 else {   const {data, lock} = workerData;   const sharedData = new Uint8Array(data);   const sharedLock = new Int32Array(lock);    do {     const lock = Atomics.wait(sharedLock, 0, THREAD_FREE); \/\/ \u0436\u0434\u0435\u043c \u043d\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u043e \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f     parentPort.postMessage({id : sharedLock[0], hash : createHash('sha256').update(sharedData).digest('hex')});     sharedLock[0] = THREAD_FREE;      \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438     Atomics.notify(sharedLock, 0, 1); \/\/ \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043e \u0441\u0432\u043e\u0435\u0439 \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u0438   }   while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b }<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u043c JavaScript-\u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u043c: <strong>\u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b<\/strong>. \u041d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430, \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u043d \u043d\u0435 \u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c.<\/p>\n<p>\u041f\u043e \u0441\u0445\u0435\u043c\u0435, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0443, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0443\u0442 <strong>\u0434\u0432\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0445 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430<\/strong>: \u043f\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u043e\u0434 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430. \u041d\u0430 \u044f\u0447\u0435\u0439\u043a\u0435 \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043c\u044b \u0436\u0434\u0435\u043c \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043d\u0435\u0439 <code>id<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0443 \u043d\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430:<\/p>\n<ul>\n<li>\n<p>\u0442\u0430\u043c \u0443\u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043d\u0435 <code>THREAD_FREE<\/code>-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u043f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 &#8212; \u0437\u043d\u0430\u0447\u0438\u0442, \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u0443\u0441\u043f\u0435\u043b \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430\u043c \u0442\u0443\u0434\u0430 \u0440\u0430\u0431\u043e\u0442\u0443;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0430\u043c \u0432\u0441\u0435 \u0435\u0449\u0435 \u0441\u0442\u043e\u0438\u0442 <code>THREAD_FREE<\/code>,  \u0438 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0435\u043c, \u043f\u043e\u043a\u0430 \u043a \u043d\u0430\u043c \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<p>\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432 \u043d\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u043c\u0435\u043d <code>.wait\/.notify<\/code> \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 &#8212; \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e <code>Atomics.store()<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c &#171;\u043e\u0431\u044b\u0447\u043d\u044b\u0435&#187; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/156\/512\/52a\/15651252a2ad963b59d04537a301bc6e.png\" alt=\"\u0421\u0445\u0435\u043c\u0430 \u0441 \u043e\u0431\u043c\u0435\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438\" title=\"\u0421\u0445\u0435\u043c\u0430 \u0441 \u043e\u0431\u043c\u0435\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438\" width=\"568\" height=\"443\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/156\/512\/52a\/15651252a2ad963b59d04537a301bc6e.png\"\/><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0441 \u043e\u0431\u043c\u0435\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438<\/figcaption><\/figure>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\/wait\">Atomics.wait()<\/a>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0436\u0434\u0430\u0442\u044c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u043e\u0442 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/v8.dev\/features\/atomics\">\u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u0432 Node.js 16<\/a> \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\/waitAsync\">Atomics.waitAsync()<\/a>.<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0432 \u043d\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u0430\u044f \u0436\u0435 \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u0438 \u043f\u0443\u043b\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438\u0437 <a href=\"\/ru\/post\/689406\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u0433\u0434\u0435 \u0432\u043c\u0435\u0441\u0442\u043e <code>.postMessage\/.on('message')<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>.notify\/.wait<\/code>:<\/p>\n<pre><code class=\"javascript\">  #shareMessage(worker, {id, data}) {     \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0438 \u0442\u0435\u043b\u043e \u0437\u0430\u0434\u0430\u0447\u0438 \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     worker.data.set(data, 0);     worker.lock[0] = id;     \/\/ \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u0435\u043c \u0435\u0433\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c     Atomics.notify(worker.lock, 0, 1);      const lock = Atomics.waitAsync(worker.lock, 0, id);     if (lock.value === 'not-equal') {       \/\/ \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c, \u0442\u043e \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443, \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u0441\u0440\u0430\u0437\u0443       this.#onMessage(worker);     }     else {       \/\/ \u0438\u043d\u0430\u0447\u0435 \u0436\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f Promise \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438       lock.value.then(result => {         this.#onMessage(worker);       });     }   }<\/code><\/pre>\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\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort , workerData } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  const THREAD_FREE = -1;  const EventEmitter = require('events'); class WorkersPool extends EventEmitter {   #queue;   #workersPool;    constructor({queue, workersPool}) {     super();      this.#queue = queue;     this.#workersPool = [...workersPool];   }    #shareMessage(worker, {id, data}) {     \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c ID \u0438 \u0442\u0435\u043b\u043e \u0437\u0430\u0434\u0430\u0447\u0438 \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430     worker.data.set(data, 0);     worker.lock[0] = id;     \/\/ \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u0435\u043c \u0435\u0433\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c     Atomics.notify(worker.lock, 0, 1);      const lock = Atomics.waitAsync(worker.lock, 0, id);     if (lock.value === 'not-equal') {       \/\/ \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c, \u0442\u043e \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443, \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u043c \u0441\u0440\u0430\u0437\u0443       this.#onMessage(worker);     }     else {       \/\/ \u0438\u043d\u0430\u0447\u0435 \u0436\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f Promise \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438       lock.value.then(result => {         this.#onMessage(worker);       });     }   }    #onMessage(worker) {     const msg = this.#queue.shift();     if (msg) {       this.#shareMessage(worker, msg);     }     else {       this.#workersPool.push(worker);     }   }    postMessage(msg) {     const worker = this.#workersPool.pop();     if (worker) {       this.#shareMessage(worker, msg);     }     else {       this.#queue.push(msg);     }   } }  if (isMainThread) {   const taskSize = 1 &lt;&lt; 16;      const tsg = hrtime();   const messages = Array(1 &lt;&lt; 12).fill().map(_ => randomBytes(taskSize));   console.log('generated:', Number(hrtime() - tsg)\/1e6 | 0, 'ms');    const hashes = messages.map(() => undefined);   let remain;    const workers = [];   let active = 1;   let tsh;    process     .on('test:start', () => {       hashes.fill();       remain = hashes.length;        workers.forEach(worker => worker.eLU = worker.performance.eventLoopUtilization());       tsh = hrtime();        \/\/ \u043f\u0443\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e       const Pow2Buffer = require('.\/Pow2Buffer');       pool = new WorkersPool({         queue : new Pow2Buffer(8, 16)       , workersPool : workers.slice(0, active)       });       \/\/ \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043f\u0443\u043b       messages.forEach((data, id) => {         pool.postMessage({id, data});       });     })     .on('test:end', () => {       const duration = hrtime() - tsh;       workers.forEach(worker => worker.util = worker.performance.eventLoopUtilization(worker.eLU).utilization);       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)         ).join(' ')       );        if (active &lt; n) {         active++;         process.emit('test:start');       }       else {         process.exit();       }     });    const n = 16;   Promise.all(     Array(n).fill().map((_, idx) => new Promise((resolve, reject) => {       \/\/ \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438       const sharedBufferData = new SharedArrayBuffer(taskSize);       const sharedBufferLock = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);       \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \"\u043f\u0443\u0441\u0442\u043e\u0435\" \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438       const lock = new Int32Array(sharedBufferLock);       lock.fill(THREAD_FREE);       \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043f\u043e\u0442\u043e\u043a \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438       const worker = new Worker(__filename,         {           workerData : {             data : sharedBufferData           , lock : sharedBufferLock           }         }       );       worker.data = new Uint8Array(sharedBufferData);       worker.lock = lock;       worker         .on('online', () => resolve(worker))         .on('message', ({id, hash}) => {           hashes[id] = hash;           if (!--remain) {             process.emit('test:end');           }         });     }))   )     .then((result) => {       workers.push(...result);       process.emit('test:start');     }); } else {   const {data, lock} = workerData;   const sharedData = new Uint8Array(data);   const sharedLock = new Int32Array(lock);    do {     const lock = Atomics.wait(sharedLock, 0, THREAD_FREE); \/\/ \u0436\u0434\u0435\u043c \u043d\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u043e \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f     parentPort.postMessage({id : sharedLock[0], hash : createHash('sha256').update(sharedData).digest('hex')});     sharedLock[0] = THREAD_FREE;      \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438     Atomics.notify(sharedLock, 0, 1); \/\/ \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u044f\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043e \u0441\u0432\u043e\u0435\u0439 \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u0438   }   while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b }<\/code><\/pre>\n<pre><code>generated: 280 ms hashed  1: 1212 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  2:  595 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  3:  433 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  4:  303 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  5:  307 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  6:  326 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  7:  302 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  8:  308 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed  9:  323 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 10:  317 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 11:  292 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 12:  311 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 13:  316 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 14:  324 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 15:  306 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 hashed 16:  298 ms | 100 |  100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100<\/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\/7ab\/14d\/f9b\/7ab14df9bed2c4716f1b3917af536c7e.png\" alt=\".wait\/.notify \u043f\u0440\u043e\u0442\u0438\u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430\" title=\".wait\/.notify \u043f\u0440\u043e\u0442\u0438\u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7ab\/14d\/f9b\/7ab14df9bed2c4716f1b3917af536c7e.png\"\/><figcaption>.wait\/.notify \u043f\u0440\u043e\u0442\u0438\u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430<\/figcaption><\/figure>\n<p>\u0412\u0441\u0435-\u0442\u0430\u043a\u0438 \u043f\u043e\u0442\u0435\u0440\u0438 \u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 &#171;\u043f\u043e\u0434\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c&#187; \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0435\u0441\u0442\u044c, \u043d\u043e \u043f\u0440\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0435\u043c \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u044f\u0434\u0435\u0440, \u043e\u043d\u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u044b. \u0422\u0430\u043a \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0441\u0440\u0435\u0434\u0438 \u0432\u0441\u0435\u0445 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043d\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/de7\/1e6\/89b\/de71e689b727e35dd8b99d237390defa.png\" width=\"1202\" height=\"471\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/de7\/1e6\/89b\/de71e689b727e35dd8b99d237390defa.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0417\u0430\u0442\u043e \u043e\u0446\u0435\u043d\u043a\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u0440\u0435\u043f\u043e\u0434\u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u043c \u0441\u044e\u0440\u043f\u0440\u0438\u0437 &#8212; \u043e\u043d\u0438 <strong>\u0432\u0441\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u044b \u043d\u0430 100%<\/strong>! \u0422\u0430\u043a \u0447\u0442\u043e, \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 &#171;\u0442\u044f\u0436\u0435\u043b\u044b\u0439&#187; \u0441\u043f\u043e\u0441\u043e\u0431?.. \u0412\u043e\u0432\u0441\u0435 \u043d\u0435\u0442! \u0412\u0435\u0434\u044c \u043c\u044b \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u043b\u0438 <strong>\u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 Event Loop<\/strong>, \u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043e\u043d\u0430 \u0432\u043f\u043e\u043b\u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0440\u0430\u0432\u043d\u0430 100%, \u0445\u043e\u0442\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0434\u043e\u043b\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u0434\u0435\u043b\u0430\u043b \u043d\u0438\u0447\u0435\u0433\u043e, \u043d\u0435 \u0433\u0440\u0443\u0437\u0438\u043b CPU, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0436\u0434\u0430\u043b \u043d\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0435.<\/p>\n<p>\u0411\u044b\u043b\u043e \u0431\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Atomics.waitAsync<\/code> \u0438 \u0432\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445, \u043d\u043e <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\/waitAsync\">MDN \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c<\/a>&#8230;<\/p>\n<blockquote>\n<p>Unlike\u00a0<code>Atomics.wait()<\/code>,\u00a0<code>waitAsync<\/code>\u00a0is non-blocking and <strong>usable on the main thread<\/strong>.<\/p>\n<\/blockquote>\n<p>\u041a\u0430\u043a \u0435\u0449\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u0441\u0445\u0435\u043c\u0435 \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438? \u041d\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0434\u043e \u0434\u0432\u0443\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u0431\u0443\u0444\u0435\u0440 \u0438 \u0438\u043c\u0435\u0442\u044c \u0434\u0432\u0435 \u044f\u0447\u0435\u0439\u043a\u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u041d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0438 \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u0410 \u044d\u0442\u0430 \u0441\u0435\u0440\u0438\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u043e\u0432 \u043f\u0440\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432 Node.js &#8212; \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430.<\/p>\n<hr\/>\n<ul>\n<li>\n<p>\u0447\u0430\u0441\u0442\u044c 1: <a href=\"\/ru\/post\/689144\/\">\u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b<\/a><\/p>\n<\/li>\n<li>\n<p>\u0447\u0430\u0441\u0442\u044c 2: <a href=\"\/ru\/post\/689406\/\">\u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043a\u0430\u043d\u0430\u043b\u044b \u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u043e\u0440<\/a><\/p>\n<\/li>\n<li>\n<p>\u0447\u0430\u0441\u0442\u044c 3: <a href=\"\/ru\/post\/689864\/\">\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c, \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/a><\/p>\n<\/li>\n<\/ul>\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\/689864\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/689864\/<\/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<p>\u0412 <a href=\"\/ru\/post\/689406\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438<\/a> \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c \u043d\u0430 \u043c\u044b\u0441\u043b\u0438, \u0447\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 <strong>\u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0438\u0445 \u0441\u0430\u043c\u0438\u0445 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0435\u0431\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong>, \u043d\u0435 \u0434\u043e\u0436\u0438\u0434\u0430\u044f\u0441\u044c, \u043f\u043e\u043a\u0430 \u0438\u0445 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442 \u043a\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b.<\/p>\n<figure class=\"full-width\"><figcaption>Threads \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438<\/figcaption><\/figure>\n<p>\u041d\u043e \u0442\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0434\u0432\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<ul>\n<li>\n<p>\u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a;<\/p>\n<\/li>\n<li>\n<p>\u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u0435\u0436\u0434\u0443 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u043d\u043e \u0438 \u0434\u0432\u0430\u0436\u0434\u044b \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0434\u0432\u0430 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e: <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%B4%D0%B5%D0%BB%D1%8F%D0%B5%D0%BC%D0%B0%D1%8F_%D0%BF%D0%B0%D0%BC%D1%8F%D1%82%D1%8C\">\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f (shared) \u043f\u0430\u043c\u044f\u0442\u044c<\/a> \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0435 (thread-safe, Atomics) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043d\u0435\u0439.<\/p>\n<h2>\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c<\/h2>\n<p>\u0412\u0441\u0435 \u043d\u0430\u0448\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u043c\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0431\u0430\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9C%D0%B5%D0%B6%D0%BF%D1%80%D0%BE%D1%86%D0%B5%D1%81%D1%81%D0%BD%D0%BE%D0%B5_%D0%B2%D0%B7%D0%B0%D0%B8%D0%BC%D0%BE%D0%B4%D0%B5%D0%B9%D1%81%D1%82%D0%B2%D0%B8%D0%B5\">IPC<\/a>. \u041d\u043e \u0432 \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0437\u0430\u0432\u0435\u0434\u043e\u043c\u043e \u0442\u0435\u0440\u044f\u0435\u043c \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u044b &#171;\u0437\u0432\u0430\u0442\u044c&#187; \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0447\u0442\u043e-\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c. \u0410 \u043d\u0435\u043b\u044c\u0437\u044f \u043b\u0438 \u043a\u0430\u043a-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0431\u0435\u0437 \u0432\u043e\u0442 \u044d\u0442\u043e\u0439 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0439 \u043e\u0431\u0432\u044f\u0437\u043a\u0438?<\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%B4%D0%B5%D0%BB%D1%8F%D0%B5%D0%BC%D0%B0%D1%8F_%D0%BF%D0%B0%D0%BC%D1%8F%D1%82%D1%8C\">\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438<\/a>, \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u043f\u043e\u0434 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u0445 \u0438\u043b\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u0445. \u0412 JavaScript \u0437\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/SharedArrayBuffer\">SharedArrayBuffer<\/a>.<\/p>\n<figure class=\"\"><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 JavaScript<\/figcaption><\/figure>\n<p>\u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 &#171;\u0437\u0430\u0434\u0430\u0447\u0438&#187; \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c\u044b, \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e <code>4K x 64KB = 256MB<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0432\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0437\u0430\u0432\u0435\u0434\u0435\u043c \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 <strong>\u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c &#171;\u043d\u043e\u043c\u0435\u0440&#187; \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong>. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u0435\u043b\u0430\u0435\u0442 <code>n++<\/code>, \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0435 \u0438 \u0438\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 <code>#n<\/code>.<\/p>\n<figure class=\"full-width\"><figcaption>\u041e\u0431\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u0430 &#171;\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439&#187; \u0437\u0430\u0434\u0430\u0447\u0438<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u044b <code>SharedArrayBuffer<\/code>-\u0441\u0435\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0438\u0445 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043f\u043e\u0442\u043e\u043a\u0430\u043c:<\/p>\n<pre><code class=\"javascript\">\/\/ ... const tasksCount = 1 &lt;&lt; 12; const taskSize   = 1 &lt;&lt; 16;  if (isMainThread) { \/\/ ...   const sharedBufferData = new SharedArrayBuffer(tasksCount * taskSize);   const sharedBufferNext = new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT);   const sharedData = new Uint8Array(sharedBufferData);   const sharedNext = new Uint32Array(sharedBufferNext); \/\/ ...       const worker = new Worker(__filename,         {           workerData : {             data : sharedBufferData           , next : sharedBufferNext           }         }       );   \/\/ ... } else {   const {data, next} = workerData;   const sharedData = new Uint8Array(data);   const sharedNext = new Uint32Array(next); \/\/ ... }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0435\u043c\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430, \u043c\u044b <strong>\u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432<\/strong> <code>transferList<\/code> \u0434\u043b\u044f \u043d\u0435-\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0438 \u0442\u0430\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h2>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u043e\u0447\u043d\u0435\u0435, \u0441 \u0435\u0433\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u043c.<\/p>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u044b &#171;\u043b\u044e\u0431\u044f\u0442&#187; \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0435, \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u0435 <strong>\u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/strong>, \u0442\u043e \u043d\u0430\u0448 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 <code>load<\/code> &#8212; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u0440\u0435\u0433\u0438\u0441\u0442\u0440, <code>inc<\/code> &#8212; \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430 \u0438 <code>store<\/code> &#8212; \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0433\u0440\u0443\u0431\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0441\u0445\u0435\u043c\u043e\u0439:<\/p>\n<figure class=\"\"><figcaption>\u0421\u0445\u0435\u043c\u0430 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u044f\u0447\u0435\u0439\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/figcaption><\/figure>\n<p>\u041d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u043e\u0442\u044f \u0431\u044b \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043c\u044b \u0443\u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u0432 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 &#8212; \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c <strong>\u043d\u0435\u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/strong>:<\/p>\n<figure class=\"full-width\"><figcaption>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0432\u0443\u0445 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 2&#8230; \u0438\u043b\u0438 \u043d\u0430 1<\/figcaption><\/figure>\n<p>\u0422\u0430\u043a \u0432\u043e\u0442, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0435\u0434\u043e\u043c\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0438 \u0431\u044b\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\">Atomics<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 <strong>\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439<\/strong> \u043d\u0430\u0434 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u044f\u0447\u0435\u0439\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043a\u0430\u0436\u0434\u0430\u044f \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u043b\u043e\u0436\u0438\u0442\u044c <strong>\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438<\/strong>: \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c &#8212; \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0438 \u0441\u043d\u0438\u043c\u0430\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443, \u043d\u0435 \u0432\u044b\u0448\u043b\u043e &#8212; \u0436\u0434\u0435\u043c, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430\u043b\u043e\u0436\u0438\u0442\u044c:<\/p>\n<figure class=\"\"><figcaption>\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u0438 Atomics-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445<\/figcaption><\/figure>\n<p>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Atomics\">Atomics<\/a> \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u043e-\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435<\/strong>: <code>add, sub, and, or, xor<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u043e\u0431\u043c\u0435\u043d\u043d\u044b\u0435<\/strong>: <code>exchange, compareExchange<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0447\u0442\u0435\u043d\u0438\u044f-\u0437\u0430\u043f\u0438\u0441\u0438<\/strong>: <code>load, store<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438<\/strong>: <code>isLockFree, notify, wait, waitAsync<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u044b\u0445 \u0442\u0440\u0435\u0445 \u0433\u0440\u0443\u043f\u043f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0430 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e, \u0445\u043e\u0440\u043e\u0448\u043e <a href=\"https:\/\/v8.dev\/features\/atomics\">\u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0431\u043b\u043e\u0433\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 V8<\/a>.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u0442\u043e\u043a\u0430 \u043d\u0435 &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442&#187;, \u0430 <code>Atomics.add<\/code>:<\/p>\n<pre><code class=\"javascript\">  parentPort.on('message', () => {     do {       const id = Atomics.add(sharedNext, 0, 1); \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442       if (id >= tasksCount) { \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         break;       }       processMessage({id, data : sharedData.subarray(id * taskSize, (id + 1) * taskSize)});     }     while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0441\u0442\u044c   });<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0431\u0449\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043b\u0430 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/subarray\">.subarray()<\/a>, \u0430 \u043d\u0435 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/slice\">.slice()<\/a>, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u043d\u0442, \u0430 \u043f\u0435\u0440\u0432\u044b\u0439 &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0442\u043e\u0442 \u0436\u0435 \u0431\u0443\u0444\u0435\u0440.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 <strong>\u043f\u043e\u0442\u043e\u043a\u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0436\u0434\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043f\u0440\u0438 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/strong> (\u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u0441 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0441\u0435\u0440\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u044b), \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u0438\u0445 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c:<\/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\u043e\u0432<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"javascript\">const {   Worker , isMainThread , parentPort , workerData } = require('node:worker_threads');  const {   randomBytes , createHash } = require('node:crypto');  const hrtime = process.hrtime.bigint;  const tasksCount = 1 &lt;&lt; 12; const taskSize   = 1 &lt;&lt; 16;  if (isMainThread) {   const tsg = hrtime();   const messages = Array(tasksCount).fill().map(_ => randomBytes(taskSize));   console.log('generated:', Number(hrtime() - tsg)\/1e6 | 0, 'ms');    const hashes = messages.map(() => undefined);   let remain;    const workers = [];   let active = 1;   let tsh;    \/\/ \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438   const sharedBufferData = new SharedArrayBuffer(tasksCount * taskSize);   const sharedBufferNext = new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT);   const sharedData = new Uint8Array(sharedBufferData);   const sharedNext = new Uint32Array(sharedBufferNext);    process     .on('test:start', () => {       hashes.fill();       remain = hashes.length;       \/\/ \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u043e\u0431\u0449\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0437\u0430\u0434\u0430\u0447       sharedData.fill(0);       Atomics.store(sharedNext, 0, 0);        workers.forEach(worker => worker.eLU = worker.performance.eventLoopUtilization());       tsh = hrtime();       \/\/ \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0435\u043b\u0430\u043c\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439       messages.forEach((data, id) => {         sharedData.set(data, id * taskSize);       });       \/\/ \"\u0434\u0435\u0440\u0433\u0430\u0435\u043c\" \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438       workers.slice(0, active).forEach(worker => worker.postMessage(undefined));     })     .on('test:end', () => {       const duration = hrtime() - tsh;       workers.forEach(worker => worker.util = worker.performance.eventLoopUtilization(worker.eLU).utilization);       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)         ).join(' ')       );        if (active &lt; n) {         active++;         process.emit('test:start');       }       else {         process.exit();       }     });    const n = 16;   Promise.all(     Array(n).fill().map(_ => new Promise((resolve, reject) => {       \/\/ \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043f\u043e\u0442\u043e\u043a \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438       const worker = new Worker(__filename,         {           workerData : {             data : sharedBufferData           , next : sharedBufferNext           }         }       );       worker         .on('online', () => resolve(worker))         .on('message', ({id, hash}) => {           hashes[id] = hash;           if (!--remain) {             process.emit('test:end');           }         });     }))   )     .then((result) => {       workers.push(...result);       process.emit('test:start');     }); } else {   \/\/ \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c TypedArray \u043d\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0445 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0445   const {data, next} = workerData;   const sharedData = new Uint8Array(data);   const sharedNext = new Uint32Array(next);      const processMessage = ({id, data}) => parentPort.postMessage({id, hash : createHash('sha256').update(data).digest('hex')});   parentPort.on('message', () => {     do {       const id = Atomics.add(sharedNext, 0, 1); \/\/ \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442       if (id >= tasksCount) { \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438         break;       }       processMessage({id, data : sharedData.subarray(id * taskSize, (id + 1) * taskSize)}); \/\/ \u043d\u0435 .slice()!     }     while (true); \/\/ \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043f\u043e\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0441\u0442\u044c   }); }<\/code><\/pre>\n<pre><code>generated: 278 ms hashed  1:  996 ms | 95 |  95  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  2:  523 ms | 91 |  91 91  0  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  3:  374 ms | 86 |  86 86 86  0  0  0  0  0  0  0  0  0  0  0  0  0 hashed  4:  297 ms | 82 |  82 82 82 82  0  0  0  0  0  0  0  0  0  0  0  0 hashed  5:  308 ms | 81 |  83 83 83 83 73  0  0  0  0  0  0  0  0  0  0  0 hashed  6:  287 ms | 79 |  82 82 82 82 71 71  0  0  0  0  0  0  0  0  0  0 hashed  7:  291 ms | 77 |  82 82 82 82 71 71 71  0  0  0  0  0  0  0  0  0 hashed  8:  290 ms | 77 |  83 83 83 83 72 72 71 71  0  0  0  0  0  0  0  0 hashed  9:  295 ms | 76 |  83 83 83 83 72 72 72 72 62  0  0  0  0  0  0  0 hashed 10:  299 ms | 73 |  82 82 82 80 72 72 72 68 61 61  0  0  0  0  0  0 hashed 11:  290 ms | 72 |  82 82 82 82 71 71 70 70 60 60 59  0  0  0  0  0 hashed 12:  307 ms | 71 |  82 82 82 81 71 71 71 71 61 60 60 60  0  0  0  0 hashed 13:  296 ms | 67 |  80 80 80 80 69 69 69 67 59 58 58 57 48  0  0  0 hashed 14:  301 ms | 67 |  82 81 81 81 70 70 70 70 60 60 59 59 49 49  0  0 hashed 15:  299 ms | 67 |  82 82 82 82 72 71 71 71 61 61 61 61 50 49<\/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-338998","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338998","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=338998"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338998\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}