{"id":347874,"date":"2023-05-25T09:03:59","date_gmt":"2023-05-25T09:03:59","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=347874"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=347874","title":{"rendered":"<span>\u041a\u044d\u0448 \u0432 JavaScript: \u043d\u0435 \u0432\u0441\u0435 Map&#8217;\u044b \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<p>\u041f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0434\u043e\u043b\u0436\u043d\u044b <strong>\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u0447\u0430\u0449\u0435, \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/strong>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 \u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f JavaScript-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f &#8212; <strong>\u043d\u0430 Map \u0438\u043b\u0438 \u043d\u0430 Object<\/strong>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/632\/e37\/2a1\/632e372a15d745757cab18a8afa93675.jpg\" alt=\"new Map()\" title=\"new Map()\" width=\"1600\" height=\"913\"><\/p>\n<div><figcaption>new Map()<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0432\u043e\u0434\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p>\u043a\u044d\u0448 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 &#171;\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u043c&#187; &#8212; \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0442\u0443\u0434\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c, \u043d\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u044d\u0448\u0430 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 &#8212; \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 8-\u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u044b\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u043d\u0430 <a href=\"https:\/\/nodejs.org\/en\/download\">Node.js<\/a> \u0442\u0435\u043a\u0443\u0449\u0435\u0439 LTS-\u0432\u0435\u0440\u0441\u0438\u0438 18.16.0.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432 \u043d\u0430\u0448\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0432\u043c\u0435\u0448\u0438\u0432\u0430\u043b\u0441\u044f Garbage Collector \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u0438\u0437 \u043a\u043e\u0434\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0435\u043c\u0443 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"bash\">node --expose-gc cache.js<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0442\u0435\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u044d\u0448 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043d\u0430 <code>Map<\/code> \u0438 \u043d\u0430 <code>Object<\/code> \u0441 \u043e\u0434\u043d\u0438\u043c\u0438 \u0438 \u0442\u0435\u043c\u0438 \u0436\u0435 \u043f\u0430\u0440\u0430\u043c\u0438 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u0442 \u0432 \u043d\u0435\u043c <strong>\u043c\u0438\u043b\u043b\u0438\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u043e\u0432<\/strong> \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">const hrtime = process.hrtime.bigint; const timeMS = ts =&gt; Number(hrtime() - ts)\/1e6 | 0;  const limit = 23; \/\/ \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u044d\u0448\u0430 \u0438\u0437 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 const content = new Array(1 &lt;&lt; limit)   .fill()   .map((_, i) =&gt; [     i.toString(16).padStart(8, '0') \/\/ key   , i                               \/\/ val   ]);  console.log('exp | map gen | obj gen | map scan | obj scan'); \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u043e\u0442 2^1 \u0434\u043e 2^23 for (let pow2 = 1; pow2 &lt;= limit; pow2++) {   const sz = 1 &lt;&lt; pow2;    const slice = content.slice(0, sz);    \/\/ \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u044d\u0448 \u043d\u0430 Map   const tsGM = hrtime();   const map = new Map(slice);   const tmGM = timeMS(tsGM);    \/\/ \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u044d\u0448 \u043d\u0430 Object   const tsGO = hrtime();   const obj = Object.fromEntries(slice);   const tmGO = timeMS(tsGO);    \/\/ \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430   const keys = new Array(1e6)     .fill()     .map(_ =&gt; ((Math.random() * sz) | 0).toString(16).padStart(8, '0'));    \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u043f\u043e Map   const tsSM = hrtime();   keys.forEach(v =&gt; map.get(v));   const tmSM = timeMS(tsSM);    \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u043f\u043e Object   const tsSO = hrtime();   keys.forEach(v =&gt; obj[v]);   const tmSO = timeMS(tsSO);    \/\/ \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Garbage Collector   gc();    console.log(     pow2.toString().padStart(3, ' ')   , '|'   , tmGM.toString().padStart(7, ' ')   , '|'   , tmGO.toString().padStart(7, ' ')   , '|'   , tmSM.toString().padStart(8, ' ')   , '|'   , tmSO.toString().padStart(8, ' ')   ); }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>exp | map gen | obj gen | map scan | obj scan   1 |       0 |       0 |       64 |      111   2 |       0 |       0 |       76 |      115   3 |       0 |       0 |       75 |      121   4 |       0 |       0 |       82 |      130   5 |       0 |       0 |       85 |      140   6 |       0 |       0 |       90 |      166   7 |       0 |       0 |       85 |      173   8 |       0 |       0 |       88 |      189   9 |       0 |       2 |       88 |      204  10 |       0 |       6 |       93 |      134  11 |       0 |       7 |       95 |      138  12 |       0 |       9 |       96 |      126  13 |       1 |      10 |      107 |      137  14 |       3 |      14 |      120 |      134  15 |       5 |      21 |      131 |      163  16 |      14 |      37 |      160 |      234  17 |      36 |      84 |      270 |      302  18 |      95 |     171 |      439 |      353  19 |     174 |     378 |      498 |      370  20 |     419 |     782 |      533 |      392  21 |     903 |    1665 |      605 |      402  22 |    1903 |    3534 |      618 |      442  23 |    4078 |   11783 |      835 |      500<\/code><\/pre>\n<p>\u0423\u0436\u0435 \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u0438. \u041d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0433\u043e\u043d\u0438\u043c \u0442\u0435\u0441\u0442 \u0442\u0440\u0438\u0436\u0434\u044b \u0438 \u0441\u0432\u0435\u0434\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5ea\/4ab\/243\/5ea4ab2430699fb029abe253ca074032.png\" alt=\"\u041f\u043e\u0438\u0441\u043a 1M \u043a\u043b\u044e\u0447\u0435\u0439 Map vs Object \u043d\u0430 3 \u0440\u0430\u0437\u043d\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:\u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f (\u043c\u0441) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u044d\u0448\u0430 2^N (\u043c\u0435\u043d\u044c\u0448\u0435 - \u043b\u0443\u0447\u0448\u0435)\" title=\"\u041f\u043e\u0438\u0441\u043a 1M \u043a\u043b\u044e\u0447\u0435\u0439 Map vs Object \u043d\u0430 3 \u0440\u0430\u0437\u043d\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:\u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f (\u043c\u0441) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u044d\u0448\u0430 2^N (\u043c\u0435\u043d\u044c\u0448\u0435 - \u043b\u0443\u0447\u0448\u0435)\" width=\"1002\" height=\"533\"><\/p>\n<div><figcaption>\u041f\u043e\u0438\u0441\u043a 1M \u043a\u043b\u044e\u0447\u0435\u0439 Map vs Object \u043d\u0430 3 \u0440\u0430\u0437\u043d\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<br \/>\u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f (\u043c\u0441) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u044d\u0448\u0430 2^N (\u043c\u0435\u043d\u044c\u0448\u0435 &#8212; \u043b\u0443\u0447\u0448\u0435)<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u044b\u0432\u043e\u0434\u044b \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043a\u0440\u0430\u0442\u043a\u043e:<\/p>\n<ul>\n<li>\n<p>\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u043b\u044f <code>Map<\/code> &#8212; \u0434\u043e 2^12 (4096) \u043a\u043b\u044e\u0447\u0435\u0439, \u043a 2^16 (64K) \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0430\u0434\u0430\u0435\u0442 \u0432 1.5 \u0440\u0430\u0437\u0430;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u0438 2^17 (128K) \u043a\u043b\u044e\u0447\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Object<\/code> \u0443\u0436\u0435 \u0432\u044b\u0433\u043e\u0434\u043d\u0435\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u043a\u0442: <code>Object<\/code> \u043d\u0430 1K..16K \u043a\u043b\u044e\u0447\u0435\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0435, \u0447\u0435\u043c \u043d\u0430 32..512.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 <code>Map<\/code> \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u0432 V8 (Node.js, Chrome, &#8230;) \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441\u043e \u0441\u0442\u0430\u0442\u044c\u0435\u0439 <a href=\"https:\/\/itnext.io\/v8-deep-dives-understanding-map-internals-45eb94a183df\">[V8 Deep Dives] Understanding Map Internals<\/a> \u0432 \u0431\u043b\u043e\u0433\u0435 <a href=\"https:\/\/apechkurov.medium.com\/\">Andrey Pechkurov<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/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\/companies\/tensor\/articles\/737432\/\"> https:\/\/habr.com\/ru\/companies\/tensor\/articles\/737432\/<\/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<p>\u041f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0434\u043e\u043b\u0436\u043d\u044b <strong>\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u0447\u0430\u0449\u0435, \u0447\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/strong>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 \u0438 \u043d\u0430 \u043a\u0430\u043a\u043e\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043b\u044f JavaScript-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f &#8212; <strong>\u043d\u0430 Map \u0438\u043b\u0438 \u043d\u0430 Object<\/strong>.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>new Map()<\/figcaption><\/div>\n<\/figure>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0432\u043e\u0434\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p>\u043a\u044d\u0448 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 &#171;\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u043c&#187; &#8212; \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0442\u0443\u0434\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c, \u043d\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c;<\/p>\n<\/li>\n<li>\n<p>\u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u044d\u0448\u0430 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 &#8212; \u0432 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 8-\u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u044b\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u043d\u0430 <a href=\"https:\/\/nodejs.org\/en\/download\">Node.js<\/a> \u0442\u0435\u043a\u0443\u0449\u0435\u0439 LTS-\u0432\u0435\u0440\u0441\u0438\u0438 18.16.0.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432 \u043d\u0430\u0448\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0432\u043c\u0435\u0448\u0438\u0432\u0430\u043b\u0441\u044f Garbage Collector \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u044f\u0432\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u0438\u0437 \u043a\u043e\u0434\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0435\u043c\u0443 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"bash\">node --expose-gc cache.js<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0442\u0435\u0441\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u044d\u0448 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043d\u0430 <code>Map<\/code> \u0438 \u043d\u0430 <code>Object<\/code> \u0441 \u043e\u0434\u043d\u0438\u043c\u0438 \u0438 \u0442\u0435\u043c\u0438 \u0436\u0435 \u043f\u0430\u0440\u0430\u043c\u0438 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u0442 \u0432 \u043d\u0435\u043c <strong>\u043c\u0438\u043b\u043b\u0438\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u043e\u0432<\/strong> \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">const hrtime = process.hrtime.bigint; const timeMS = ts =&gt; Number(hrtime() - ts)\/1e6 | 0;  const limit = 23; \/\/ \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u044d\u0448\u0430 \u0438\u0437 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 const content = new Array(1 &lt;&lt; limit)   .fill()   .map((_, i) =&gt; [     i.toString(16).padStart(8, '0') \/\/ key   , i                               \/\/ val   ]);  console.log('exp | map gen | obj gen | map scan | obj scan'); \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u043e\u0442 2^1 \u0434\u043e 2^23 for (let pow2 = 1; pow2 &lt;= limit; pow2++) {   const sz = 1 &lt;&lt; pow2;    const slice = content.slice(0, sz);    \/\/ \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u044d\u0448 \u043d\u0430 Map   const tsGM = hrtime();   const map = new Map(slice);   const tmGM = timeMS(tsGM);    \/\/ \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043a\u044d\u0448 \u043d\u0430 Object   const tsGO = hrtime();   const obj = Object.fromEntries(slice);   const tmGO = timeMS(tsGO);    \/\/ \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430   const keys = new Array(1e6)     .fill()     .map(_ =&gt; ((Math.random() * sz) | 0).toString(16).padStart(8, '0'));    \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u043f\u043e Map   const tsSM = hrtime();   keys.forEach(v =&gt; map.get(v));   const tmSM = timeMS(tsSM);    \/\/ \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u043f\u043e Object   const tsSO = hrtime();   keys.forEach(v =&gt; obj[v]);   const tmSO = timeMS(tsSO);    \/\/ \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Garbage Collector   gc();    console.log(     pow2.toString().padStart(3, ' ')   , '|'   , tmGM.toString().padStart(7, ' ')   , '|'   , tmGO.toString().padStart(7, ' ')   , '|'   , tmSM.toString().padStart(8, ' ')   , '|'   , tmSO.toString().padStart(8, ' ')   ); }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>exp | map gen | obj gen | map scan | obj scan   1 |       0 |       0 |       64 |      111   2 |       0 |       0 |       76 |      115   3 |       0 |       0 |       75 |      121   4 |       0 |       0 |       82 |      130   5 |       0 |       0 |       85 |      140   6 |       0 |       0 |       90 |      166   7 |       0 |       0 |       85 |      173   8 |       0 |       0 |       88 |      189   9 |       0 |       2 |       88 |      204  10 |       0 |       6 |       93 |      134  11 |       0 |       7 |       95 |      138  12 |       0 |       9 |       96 |      126  13 |       1 |      10 |      107 |      137  14 |       3 |      14 |      120 |      134  15 |       5 |      21 |      131 |      163  16 |      14 |      37 |      160 |      234  17 |      36 |      84 |      270 |      302  18 |      95 |     171 |      439 |      353  19 |     174 |     378 |      498 |      370  20 |     419 |     782 |      533 |      392  21 |     903 |    1665 |      605 |      402  22 |    1903 |    3534 |      618 |      442  23 |    4078 |   11783 |      835 |      500<\/code><\/pre>\n<p>\u0423\u0436\u0435 \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u0438. \u041d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0433\u043e\u043d\u0438\u043c \u0442\u0435\u0441\u0442 \u0442\u0440\u0438\u0436\u0434\u044b \u0438 \u0441\u0432\u0435\u0434\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u041f\u043e\u0438\u0441\u043a 1M \u043a\u043b\u044e\u0447\u0435\u0439 Map vs Object \u043d\u0430 3 \u0440\u0430\u0437\u043d\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<br \/>\u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f (\u043c\u0441) \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u044d\u0448\u0430 2^N (\u043c\u0435\u043d\u044c\u0448\u0435 &#8212; \u043b\u0443\u0447\u0448\u0435)<\/figcaption><\/div>\n<\/figure>\n<p>\u0412\u044b\u0432\u043e\u0434\u044b \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043a\u0440\u0430\u0442\u043a\u043e:<\/p>\n<ul>\n<li>\n<p>\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u043b\u044f <code>Map<\/code> &#8212; \u0434\u043e 2^12 (4096) \u043a\u043b\u044e\u0447\u0435\u0439, \u043a 2^16 (64K) \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0430\u0434\u0430\u0435\u0442 \u0432 1.5 \u0440\u0430\u0437\u0430;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u0438 2^17 (128K) \u043a\u043b\u044e\u0447\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Object<\/code> \u0443\u0436\u0435 \u0432\u044b\u0433\u043e\u0434\u043d\u0435\u0435;<\/p>\n<\/li>\n<li>\n<p>\u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u043a\u0442: <code>Object<\/code> \u043d\u0430 1K..16K \u043a\u043b\u044e\u0447\u0435\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u0435, \u0447\u0435\u043c \u043d\u0430 32..512.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 <code>Map<\/code> \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u0432 V8 (Node.js, Chrome, &#8230;) \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441\u043e \u0441\u0442\u0430\u0442\u044c\u0435\u0439 <a href=\"https:\/\/itnext.io\/v8-deep-dives-understanding-map-internals-45eb94a183df\">[V8 Deep Dives] Understanding Map Internals<\/a> \u0432 \u0431\u043b\u043e\u0433\u0435 <a href=\"https:\/\/apechkurov.medium.com\/\">Andrey Pechkurov<\/a>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/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\/companies\/tensor\/articles\/737432\/\"> https:\/\/habr.com\/ru\/companies\/tensor\/articles\/737432\/<\/a><br \/><\/br><\/br><\/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-347874","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347874","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=347874"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/347874\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}