{"id":461220,"date":"2025-05-27T21:01:52","date_gmt":"2025-05-27T21:01:52","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=461220"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=461220","title":{"rendered":"<span>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0432\u043e\u0439 await fetch \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u2014 \u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c<\/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><strong>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/strong><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u0435\u0437\u043e\u0431\u0438\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u0447\u043a\u0430 <code>await fetch()<\/code> \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u0440\u043c\u043e\u0437, \u0433\u0434\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0430 \u0437\u0430\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u0440\u0430\u0433\u043e\u0446\u0435\u043d\u043d\u044b\u0435 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b \u2014 \u0438 \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c.<\/p>\n<h3>\u0425\u043e\u043b\u043e\u0434\u043d\u044b\u0435 TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f: 200 \u043c\u0441 \u043d\u0430 \u0440\u043e\u0432\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435<\/h3>\n<p>\u0421\u0438\u043c\u043f\u0442\u043e\u043c: \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a API \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445, \u0430 \u043f\u0440\u0438 \u0431\u0443\u0440\u0441\u0442\u0435 <img decoding=\"async\" class=\"formula inline\" source=\"t\u2089\u2085\" alt=\"t\u2089\u2085\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/4\/4c\/4cb\/4cb32166fcded95f49dea7c0930ec08d.svg\" width=\"auto\" height=\"auto\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/4\/4c\/4cb\/4cb32166fcded95f49dea7c0930ec08d.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/formulas\/4\/4c\/4cb\/4cb32166fcded95f49dea7c0930ec08d.svg 781w\" loading=\"lazy\" decode=\"async\"\/> \u0441\u043a\u0430\u0447\u0435\u0442 \u0432 \u043a\u043e\u0441\u043c\u043e\u0441.<\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 <code>fetch()<\/code> \u0432 \u043b\u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0441\u043e\u043a\u0435\u0442: 1\u00d7DNS, 1\u00d7TCP-handshake, 1\u00d7TLS. \u0421\u0440\u0435\u0434\u043d\u0438\u0439 RTT \u0432 \u0415\u0432\u0440\u043e\u043f\u0435 ~50 \u043c\u0441, \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u043c \u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u0442\u043d\u0438 \u043b\u0438\u0448\u043d\u0438\u0445 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 3-\u043a\u0440\u0430\u0442\u043d\u044b\u0439 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043f\u0440\u0438 reuse 1000 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0442\u0438\u0432 \u00ab\u043e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u2014 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441\u00bb.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">\/\/ Node 22+, global fetch \u0443\u0436\u0435 \u0435\u0441\u0442\u044c import { Agent } from 'undici';  const api = new Agent({   keepAliveTimeout: 30_000,   \/\/ \u0434\u0435\u0440\u0436\u0438\u043c \u0441\u043e\u043a\u0435\u0442 30 \u0441   connections: 100,           \/\/ \u043f\u0443\u043b });  const res = await fetch('https:\/\/api.payments.local\/v1\/orders', { dispatcher: api });<\/code><\/pre>\n<p>\u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u2014 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Connection: keep-alive<\/code> \u043f\u043b\u044e\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 HTTP\/2\/3. \u041d\u0430 \u0444\u0440\u043e\u043d\u0442\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>&lt;link rel=\"preconnect\" href=\"https:\/\/api.payments.local\"&gt;<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c handshake \u0434\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u043a\u0430.<\/p>\n<p>Undici \u0434\u0435\u0440\u0436\u0438\u0442 keep-alive \u043d\u0430\u0442\u0438\u0432\u043d\u043e \u0438 \u0434\u0430\u0451\u0442 \u043f\u043e\u0447\u0442\u0438 10\u00d7 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043a core-HTTP-\u043a\u043b\u0438\u0435\u043d\u0442\u0443.<\/p>\n<h3>DNS + TLS: \u0434\u0432\u0430 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0434\u0440\u0430\u043a\u043e\u043d\u0447\u0438\u043a\u0430<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u0441 Keep-Alive \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a <em>\u0434\u0440\u0443\u0433\u043e\u043c\u0443<\/em> \u0434\u043e\u043c\u0435\u043d\u0443 \u0441\u043d\u043e\u0432\u0430 \u0434\u043b\u0438\u043d\u043d\u044b\u0439.<\/p>\n<p><strong>DNS-lookup<\/strong> \u2014 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 JS-\u043f\u043e\u0442\u043e\u043a \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 (\u0434\u043e 100 \u043c\u0441 \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445). TLS-handshake \u2014 \u0442\u0440\u0438 round-trip \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0443 TCP.<\/p>\n<p>\u041b\u0443\u0447\u0448\u0435 \u044f\u0432\u043d\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c DNS-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0442\u044c <code>maxSockets<\/code>, \u0435\u0441\u043b\u0438 \u0443 \u0412\u0430\u0441 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043c\u0451\u043d.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"bash\"># nginx.conf resolver 9.9.9.9 valid=300s;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ Node const agent = new Agent({ connect: { lookup: dnsCache.lookup }});<\/code><\/pre>\n<p>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u2014 QUIC\/HTTP-3 \u0441 0-RTT, \u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u043a\u0441\u0438 \u2014 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043c\u0435\u0440\u043e\u0432 HTTP\/2 vs HTTP\/1.1 \u043f\u043e\u0440\u043e\u0439 \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b.<\/p>\n<h3>response.json() \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 event-loop<\/h3>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0434\u0430\u0451\u0442 5-10 \u041c\u0411 JSON \u2014 \u0438 \u0412\u0430\u0448 API-\u0440\u043e\u0443\u0442 \u0437\u0430\u0432\u0438\u0441\u0430\u0435\u0442, CPU \u043f\u0440\u044b\u0433\u0430\u0435\u0442 \u0434\u043e 100 %.<\/p>\n<p><code>Response.prototype.json()<\/code> \u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043f\u043e\u0442\u043e\u043a \u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>JSON.parse<\/code>, \u043c\u043e\u043d\u043e\u043f\u043e\u043b\u0438\u0437\u0438\u0440\u0443\u044f \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u044f\u0434\u0440\u043e. <\/p>\n<h4>\u0424\u0438\u043a\u0441: \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433<\/h4>\n<pre><code class=\"javascript\">import { parse } from 'stream-json'; import { chain } from 'stream-chain';  const pipeline = chain([   response.body,  \/\/ ReadableStream \u0438\u0437 fetch()   parse(),   ({ key, value }) =&gt; { \/* ...\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b... *\/ }, ]); await finished(pipeline);<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0430\u0437 \u043e\u0442 WebStreams \u0432 \u043f\u043e\u043b\u044c\u0437\u0443 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 Node-\u0441\u0442\u0440\u0438\u043c\u043e\u0432 \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0447\u0442\u0435\u043d\u0438\u0435 \u043d\u0430 60\u201390 %.<\/p>\n<h3>\u0412\u0435\u0441 \u043e\u0442\u0432\u0435\u0442\u0430: \u0441\u0436\u0430\u0442\u0438\u0435 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u044b<\/h3>\n<p>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0441\u0435\u0442\u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f, \u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0434\u043e\u043b\u0433\u0430\u044f, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u043e\u043c 4G.<\/p>\n<p>\u0422\u0430\u043a \u0431\u044b\u0432\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 API \u043e\u0442\u0434\u0430\u0451\u0442 \u00ab\u0433\u043e\u043b\u044b\u0439\u00bb JSON = 2\u00d7-3\u00d7 \u043b\u0438\u0448\u043d\u0438\u0445 \u0431\u0430\u0439\u0442. \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 Brotli, \u0430 \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0435 \u043f\u0440\u043e\u0441\u0438\u0442. \u041e\u0442\u0434\u0430\u0451\u0442\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 PNG \u0432\u043c\u0435\u0441\u0442\u043e WebP\/AVIF.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">\/\/ \u0444\u0440\u043e\u043d\u0442 await fetch(url, {   headers: { 'Accept-Encoding': 'br, gzip' } });  \/\/ backend (Fastify) fastify.register(require('@fastify\/compress'), {   brotliOptions: { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5 } } });<\/code><\/pre>\n<p> Brotli \u043d\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 payload \u0434\u0430\u0451\u0442 \u0434\u043e 25 % \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0442\u0440\u0430\u0444\u0438\u043a\u0430 \u0441\u0432\u0435\u0440\u0445 Gzip.<\/p>\n<h3>Await-\u0432-\u0446\u0438\u043a\u043b\u0435<\/h3>\n<p>\u0415\u0441\u0442\u044c 100 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u043e \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u2248 100 \u00d7 \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430:<\/p>\n<pre><code class=\"javascript\">for (const id of ids) {   const res = await fetch(`\/api\/item\/${id}`);   items.push(await res.json()); }<\/code><\/pre>\n<p>\u0412\u044b \u043b\u0438\u043d\u0435\u0430\u0440\u0438\u0437\u0443\u0435\u0442\u0435 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">const concurrency = 10;        \/\/ \u0431\u0435\u0440\u0435\u0436\u0451\u043c \u0431\u044d\u043a\u0435\u043d\u0434 const queue = [...ids]; const results = [];  await Promise.all(   Array.from({ length: concurrency }, async function worker() {     while (queue.length) {       const id = queue.pop();       const res = await fetch(`\/api\/item\/${id}`);       results.push(await res.json());     }   }) );<\/code><\/pre>\n<h3>\u041c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442: fetch vs undici.request<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u043f\u0440\u0438 \u0432\u0441\u0435\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u0445<img decoding=\"async\" class=\"formula inline\" source=\" t\u2089\u2085 \" alt=\" t\u2089\u2085 \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/904\/d49\/e82\/904d49e82a03e89d9abd958832fe32d3.svg\" width=\"17\" height=\"21\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/904\/d49\/e82\/904d49e82a03e89d9abd958832fe32d3.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/904\/d49\/e82\/904d49e82a03e89d9abd958832fe32d3.svg 781w\" loading=\"lazy\" decode=\"async\"\/>\u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b.<\/p>\n<p>Node-fetch \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u00ab\u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 \u043e\u0431\u0451\u0440\u0442\u043a\u0438\u00bb: web-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c &gt; \u043b\u0438\u0448\u043d\u0438\u0439 GC, \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. Matt Pocock \u0438\u0437\u043c\u0435\u0440\u0438\u043b: <code>undici.request<\/code> \u2248 2\u20133\u00d7 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 built-in <code>fetch<\/code>.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">import { request } from 'undici';  const { body } = await request('https:\/\/inventory.local\/items', {   method: 'POST',   body: JSON.stringify(payload),   headers: { 'content-type': 'application\/json' }, }); const data = await body.json();        \/\/ \u0432\u0441\u0451 \u0435\u0449\u0451 \u0443\u0434\u043e\u0431\u043d\u043e<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0412\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c HTTP-\u0432\u044b\u0437\u043e\u0432\u044b, \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 <code>undici.request<\/code> \u0434\u0430\u0441\u0442 3\u00d7 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435.<\/p>\n<h3> CORS-preflight<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043a \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u043c\u0443 API \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u0442 +150\u2013250 \u043c\u0441. \u0412 Perf-\u0442\u0430\u0431 \u00ab\u0446\u0430\u0440\u0438\u0442\u00bb \u043b\u0438\u0448\u043d\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 <code>OPTIONS<\/code>, \u0430 \u0441\u043b\u0435\u0434\u043e\u043c \u0443\u0436\u0435 \u0438\u0434\u0451\u0442 \u0412\u0430\u0448 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 <code>GET\/POST<\/code>.<\/p>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 <strong>preflight-\u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043f\u0440\u0438 \u043d\u0435\u0441-simple \u043c\u0435\u0442\u043e\u0434\u0430\u0445\/\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445 (\u043b\u044e\u0431\u043e\u0435 <code>Content-Type<\/code> \u043a\u0440\u043e\u043c\u0435 <code>application\/x-www-form-urlencoded<\/code>, \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 headers, <code>credentials: true<\/code> \u0438 \u0442.\u043f.). \u041f\u043e\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u043e\u0442\u0432\u0435\u0442\u0438\u0442 200 OK, \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u2014 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 RTT \u043f\u043b\u044e\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434\u0435. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<ol>\n<li>\n<p><strong>\u0423\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>: <code>GET<\/code> \u0438\u043b\u0438 <code>POST<\/code> \u0441 <code>Content-Type: text\/plain<\/code> (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043e\u043a) \u0438 \u0431\u0435\u0437 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u044d\u0448\u0438\u0440\u0443\u0439\u0442\u0435 preflight<\/strong> \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"javascript\">\/\/ Express + cors app.options('*', cors({   origin: true,   credentials: true,   maxAge: 86_400,      \/\/ 24 \u0447 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0430\u0442\u044c OPTIONS }));<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u0421\u0445\u043b\u043e\u043f\u043d\u0438\u0442\u0435 \u0434\u043e\u043c\u0435\u043d\u044b<\/strong> \u2014 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0438\u0442\u0435 API \u043d\u0430 \u043f\u043e\u0434\u043f\u0443\u0442\u044c (<code>\/api<\/code>) \u0442\u043e\u0433\u043e \u0436\u0435 origin, \u0447\u0442\u043e\u0431\u044b CORS \u0438\u0441\u0447\u0435\u0437 \u0432\u043e\u0432\u0441\u0435.<\/p>\n<\/li>\n<\/ol>\n<h3>HOL-\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0432 HTTP\/2: \u043e\u0434\u0438\u043d \u0436\u0438\u0440\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u0432\u0441\u0435\u0445<\/h3>\n<p>\u041d\u0430 HTTP\/2-\u0431\u044d\u043a\u0435\u043d\u0434\u0435 \u0432\u0441\u043f\u043b\u0435\u0441\u043a\u0438 <img decoding=\"async\" class=\"formula inline\" source=\"t\u2089\u2089\" alt=\"t\u2089\u2089\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ee\/d02\/ca5\/3eed02ca5aab63959cb358088d06d5a1.svg\" width=\"17\" height=\"21\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ee\/d02\/ca5\/3eed02ca5aab63959cb358088d06d5a1.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ee\/d02\/ca5\/3eed02ca5aab63959cb358088d06d5a1.svg 781w\" loading=\"lazy\" decode=\"async\"\/> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0440\u043e\u0432\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0437\u0430\u0433\u0440\u0443\u0437\u043e\u043a\/\u043e\u0442\u0434\u0430\u0447 (\u0444\u0430\u0439\u043b\u044b &gt; 5 \u041c\u0411). \u041b\u043e\u0433\u0438 \u043f\u0443\u0441\u0442\u044b, CPU \u2014 \u043d\u043e\u0440\u043c, \u0441\u0435\u0442\u044c \u2014 \u043d\u043e\u0440\u043c, \u043d\u043e \u0444\u0440\u043e\u043d\u0442 \u0444\u0440\u0438\u0437\u0438\u0442.<\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0442\u0435\u0440\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 <strong>TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442<\/strong> \u0432\u0441\u0451, \u0447\u0442\u043e \u0438\u0434\u0451\u0442 \u043f\u043e\u0441\u043b\u0435 \u0433\u043e\u043b\u043e\u0432\u044b. \u0412 HTTP\/2 \u0432\u0441\u0435 \u0441\u0442\u0440\u0438\u043c\u044b \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 <strong>\u043e\u0434\u0438\u043d TCP-\u043f\u043e\u0442\u043e\u043a<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 \u043d\u0430 file-upload \u0441\u0442\u043e\u043f\u043e\u0440\u0438\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 API-\u0432\u044b\u0437\u043e\u0432\u044b. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<p><strong>\u0420\u0430\u0437\u043d\u0435\u0441\u0438\u0442\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0435 \u0438 \u043c\u0435\u043b\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b<\/strong>: \u0434\u0435\u0440\u0436\u0438\u0442\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0433\u0435\u043d\u0442\/\u0434\u043e\u043c\u0435\u043d\/\u043f\u043e\u0440\u0442 \u043f\u043e\u0434 \u0442\u043e\u043b\u0441\u0442\u044b\u0435 \u0430\u043f\u043b\u043e\u0443\u0434\u044b.<\/p>\n<pre><code class=\"json\">const bulkAgent = new Agent({ maxStreamsPerConnection: 1 }); \/\/ \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 await fetch(largeUrl, { dispatcher: bulkAgent, body: bigFile });<\/code><\/pre>\n<p><strong>\u0412\u043a\u043b\u044e\u0447\u0438\u0442\u0435 HTTP\/3 (QUIC)<\/strong> \u2014 \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e UDP, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c. <strong>\u0422\u044e\u043d\u044c\u0442\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0438\u0437\u0430\u0446\u0438\u044e<\/strong>: <code>http2_max_concurrent_streams<\/code>, <code>priority<\/code>-\u0445\u0438\u043d\u0442\u044b \u0438\u043b\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 WAF-\u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u0430\u043a\u0435\u0442-\u043f\u043e\u0442\u0435\u0440\u044f \u043d\u0435 \u043a\u043b\u0430\u043b\u0430 \u0432\u0441\u0451 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u0441\u0442\u0435\u043d\u0434\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 upload-\u0440\u0443\u0447\u0435\u043a \u043d\u0430 HTTP\/3 \u0434\u0430\u043b\u043e \u201345 % \u043a \u043c\u0435\u0434\u0438\u0430\u043d\u0435 \u0438 \u201379 % \u043a p99.<\/p>\n<h3>JSON.stringify \u043f\u0435\u0440\u0435\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u043e\u0439<\/h3>\n<p>\u041f\u0440\u0438 \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u043c <code>POST \/bulk<\/code> \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0435 100 % CPU, \u0430 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0442\u0440\u0430\u0444\u0438\u043a \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043f\u043e\u0437\u0434\u043d\u043e: \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043a\u0438\u043f\u0438\u0442 \u0435\u0449\u0451 \u0434\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e <code>fetch()<\/code>.<\/p>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u043d\u0435 \u0443\u043b\u0435\u0442\u0438\u0442, \u043f\u043e\u043a\u0430 \u0412\u044b \u043d\u0435 <strong>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0435<\/strong> payload. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0432\u044f\u0437\u043a\u0430<\/p>\n<pre><code class=\"javascript\">await fetch(url, { body: JSON.stringify(hugeObject) });<\/code><\/pre>\n<p>\u043e\u0434\u043d\u0438\u043c \u043c\u0430\u0445\u043e\u043c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044f event-loop. \u0410 \u0435\u0441\u043b\u0438 \u0412\u044b \u0435\u0449\u0451 \u0438 \u043a\u043b\u043e\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u0434\u0430\u0432 <code>body: {...data}<\/code>, \u0432\u0440\u0435\u043c\u044f \u0443\u0434\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412 Node 24\/Chromium 122 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d <code><strong>structuredClone()<\/strong><\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 deep-copy, \u043d\u043e \u043e\u043d \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0435\u043d. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<ol>\n<li>\n<p><strong>\u0421\u0442\u0440\u0438\u043c\u043e\u0432\u044b\u0439 multipart\/NDJSON-upload<\/strong> \u2014 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u0443\u0441\u043e\u0447\u043d\u043e:<\/p>\n<pre><code class=\"javascript\">import { Readable } from 'node:stream'; const enc = new TextEncoder(); const stream = Readable.from(   bigArray.map(x =&gt; enc.encode(JSON.stringify(x) + '\\n')) ); await fetch('\/bulk\/ingest', { method: 'POST', body: stream });<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 \u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e &lt; 50 \u041c\u0411 \u0434\u0430\u0436\u0435 \u043d\u0430 1 \u0413\u0411 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0438\u0448\u0438\u0442\u0435 \u0432 BSON\/MsgPack<\/strong>, \u043c\u0438\u043d\u0443\u044f stringify.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043e\u0442 \u043b\u0438\u0448\u043d\u0435\u0433\u043e deep-copy<\/strong> \u2014 \u0432\u043c\u0435\u0441\u0442\u043e<\/p>\n<pre><code class=\"javascript\">const safe = structuredClone(huge);<\/code><\/pre>\n<p>\u0447\u0430\u0449\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Schemas.<\/p>\n<\/li>\n<\/ol>\n<h3>\u0418\u0442\u0430\u043a<\/h3>\n<p><strong>\u0418\u0437\u043c\u0435\u0440\u044f\u0439\u0442\u0435<\/strong>. <code>npx bench-rest<\/code>, <code>autocannon<\/code>, \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0439 PerformanceTab. <strong>\u0427\u0438\u043d\u0438\u0442\u0435 \u043f\u043e \u0441\u043b\u043e\u044f\u043c<\/strong>: \u0441\u0435\u0442\u044c &gt; \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b &gt; \u043a\u043b\u0438\u0435\u043d\u0442 &gt; \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f &gt; \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. <strong>\u041d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432<\/strong>: GraphQL-over-HTTP\/2, gRPC-web, msgpack \u0432\u043c\u0435\u0441\u0442\u043e JSON. <strong>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435<\/strong>. \u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 Artillery \u0432 CI, \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0430\u043b\u0435\u0440\u0442\u044b \u043d\u0430 p95&gt;200 \u043c\u0441.<\/p>\n<hr\/>\n<p>\u0412\u0441\u0435\u0445, \u043a\u0442\u043e \u0438\u0449\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0443\u0442\u044c \u043a \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u044b\u043c \u0437\u043d\u0430\u043d\u0438\u044f\u043c, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u0441\u0435\u0440\u0438\u044e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 <a href=\"https:\/\/otus.pw\/Y7TK\/\">\u043a\u0443\u0440\u0441\u0430 Fullstack Developer<\/a>:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/mUPPe\/\">Web-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0438 Shadow DOM: \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 UI\u2011\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/a>\u00a0\u2014 <em>28\u00a0\u043c\u0430\u044f<\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/uXTg\/\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u00a0API \u0434\u043b\u044f\u00a0\u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445: \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u00a0\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443<\/a>\u00a0\u2014 <em>3\u00a0\u0438\u044e\u043d\u044f <\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/rrGwH\/\">\u041c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441\u00a0HTML \u0438 CSS \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e JavaScript<\/a>\u00a0\u2014 <em>12\u00a0\u0438\u044e\u043d\u044f<\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/Y9aL\/\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441\u00a0\u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 html \u0442\u0435\u0433\u0430\u043c\u0438 \u0438 \u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438<\/a>\u00a0\u2014 <em>17\u00a0\u0438\u044e\u043d\u044f<\/em><\/p>\n<\/li>\n<\/ul>\n<\/div>\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\/articles\/912854\/\"> https:\/\/habr.com\/ru\/articles\/912854\/<\/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><strong>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/strong><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u0435\u0437\u043e\u0431\u0438\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u0447\u043a\u0430 <code>await fetch()<\/code> \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u0440\u043c\u043e\u0437, \u0433\u0434\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0430 \u0437\u0430\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u0440\u0430\u0433\u043e\u0446\u0435\u043d\u043d\u044b\u0435 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b \u2014 \u0438 \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c.<\/p>\n<h3>\u0425\u043e\u043b\u043e\u0434\u043d\u044b\u0435 TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f: 200 \u043c\u0441 \u043d\u0430 \u0440\u043e\u0432\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435<\/h3>\n<p>\u0421\u0438\u043c\u043f\u0442\u043e\u043c: \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a API \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445, \u0430 \u043f\u0440\u0438 \u0431\u0443\u0440\u0441\u0442\u0435  \u0441\u043a\u0430\u0447\u0435\u0442 \u0432 \u043a\u043e\u0441\u043c\u043e\u0441.<\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 <code>fetch()<\/code> \u0432 \u043b\u043e\u0431 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0441\u043e\u043a\u0435\u0442: 1\u00d7DNS, 1\u00d7TCP-handshake, 1\u00d7TLS. \u0421\u0440\u0435\u0434\u043d\u0438\u0439 RTT \u0432 \u0415\u0432\u0440\u043e\u043f\u0435 ~50 \u043c\u0441, \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u043c \u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u0442\u043d\u0438 \u043b\u0438\u0448\u043d\u0438\u0445 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 3-\u043a\u0440\u0430\u0442\u043d\u044b\u0439 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043f\u0440\u0438 reuse 1000 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0442\u0438\u0432 \u00ab\u043e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u2014 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441\u00bb.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">\/\/ Node 22+, global fetch \u0443\u0436\u0435 \u0435\u0441\u0442\u044c import { Agent } from 'undici';  const api = new Agent({   keepAliveTimeout: 30_000,   \/\/ \u0434\u0435\u0440\u0436\u0438\u043c \u0441\u043e\u043a\u0435\u0442 30 \u0441   connections: 100,           \/\/ \u043f\u0443\u043b });  const res = await fetch('https:\/\/api.payments.local\/v1\/orders', { dispatcher: api });<\/code><\/pre>\n<p>\u0412 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u2014 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>Connection: keep-alive<\/code> \u043f\u043b\u044e\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 HTTP\/2\/3. \u041d\u0430 \u0444\u0440\u043e\u043d\u0442\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c <code>&lt;link rel=\"preconnect\" href=\"https:\/\/api.payments.local\"&gt;<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c handshake \u0434\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u043a\u0430.<\/p>\n<p>Undici \u0434\u0435\u0440\u0436\u0438\u0442 keep-alive \u043d\u0430\u0442\u0438\u0432\u043d\u043e \u0438 \u0434\u0430\u0451\u0442 \u043f\u043e\u0447\u0442\u0438 10\u00d7 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043a core-HTTP-\u043a\u043b\u0438\u0435\u043d\u0442\u0443.<\/p>\n<h3>DNS + TLS: \u0434\u0432\u0430 \u0441\u043a\u0440\u044b\u0442\u044b\u0445 \u0434\u0440\u0430\u043a\u043e\u043d\u0447\u0438\u043a\u0430<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u0441 Keep-Alive \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a <em>\u0434\u0440\u0443\u0433\u043e\u043c\u0443<\/em> \u0434\u043e\u043c\u0435\u043d\u0443 \u0441\u043d\u043e\u0432\u0430 \u0434\u043b\u0438\u043d\u043d\u044b\u0439.<\/p>\n<p><strong>DNS-lookup<\/strong> \u2014 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 JS-\u043f\u043e\u0442\u043e\u043a \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 (\u0434\u043e 100 \u043c\u0441 \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445). TLS-handshake \u2014 \u0442\u0440\u0438 round-trip \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0443 TCP.<\/p>\n<p>\u041b\u0443\u0447\u0448\u0435 \u044f\u0432\u043d\u043e \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c DNS-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0442\u044c <code>maxSockets<\/code>, \u0435\u0441\u043b\u0438 \u0443 \u0412\u0430\u0441 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043c\u0451\u043d.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"bash\"># nginx.conf resolver 9.9.9.9 valid=300s;<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ Node const agent = new Agent({ connect: { lookup: dnsCache.lookup }});<\/code><\/pre>\n<p>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u2014 QUIC\/HTTP-3 \u0441 0-RTT, \u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0441\u0442\u0440\u0430\u043d\u043d\u043e\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u043a\u0441\u0438 \u2014 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0437\u0430\u043c\u0435\u0440\u043e\u0432 HTTP\/2 vs HTTP\/1.1 \u043f\u043e\u0440\u043e\u0439 \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b.<\/p>\n<h3>response.json() \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 event-loop<\/h3>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0434\u0430\u0451\u0442 5-10 \u041c\u0411 JSON \u2014 \u0438 \u0412\u0430\u0448 API-\u0440\u043e\u0443\u0442 \u0437\u0430\u0432\u0438\u0441\u0430\u0435\u0442, CPU \u043f\u0440\u044b\u0433\u0430\u0435\u0442 \u0434\u043e 100 %.<\/p>\n<p><code>Response.prototype.json()<\/code> \u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043f\u043e\u0442\u043e\u043a \u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>JSON.parse<\/code>, \u043c\u043e\u043d\u043e\u043f\u043e\u043b\u0438\u0437\u0438\u0440\u0443\u044f \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u044f\u0434\u0440\u043e. <\/p>\n<h4>\u0424\u0438\u043a\u0441: \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0439 \u043f\u0430\u0440\u0441\u0438\u043d\u0433<\/h4>\n<pre><code class=\"javascript\">import { parse } from 'stream-json'; import { chain } from 'stream-chain';  const pipeline = chain([   response.body,  \/\/ ReadableStream \u0438\u0437 fetch()   parse(),   ({ key, value }) =&gt; { \/* ...\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b... *\/ }, ]); await finished(pipeline);<\/code><\/pre>\n<p>\u041e\u0442\u043a\u0430\u0437 \u043e\u0442 WebStreams \u0432 \u043f\u043e\u043b\u044c\u0437\u0443 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 Node-\u0441\u0442\u0440\u0438\u043c\u043e\u0432 \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0447\u0442\u0435\u043d\u0438\u0435 \u043d\u0430 60\u201390 %.<\/p>\n<h3>\u0412\u0435\u0441 \u043e\u0442\u0432\u0435\u0442\u0430: \u0441\u0436\u0430\u0442\u0438\u0435 \u0438 \u0444\u043e\u0440\u043c\u0430\u0442\u044b<\/h3>\n<p>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0441\u0435\u0442\u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f, \u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0434\u043e\u043b\u0433\u0430\u044f, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043d\u0430 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u043e\u043c 4G.<\/p>\n<p>\u0422\u0430\u043a \u0431\u044b\u0432\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 API \u043e\u0442\u0434\u0430\u0451\u0442 \u00ab\u0433\u043e\u043b\u044b\u0439\u00bb JSON = 2\u00d7-3\u00d7 \u043b\u0438\u0448\u043d\u0438\u0445 \u0431\u0430\u0439\u0442. \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 Brotli, \u0430 \u043a\u043b\u0438\u0435\u043d\u0442 \u043d\u0435 \u043f\u0440\u043e\u0441\u0438\u0442. \u041e\u0442\u0434\u0430\u0451\u0442\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 PNG \u0432\u043c\u0435\u0441\u0442\u043e WebP\/AVIF.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">\/\/ \u0444\u0440\u043e\u043d\u0442 await fetch(url, {   headers: { 'Accept-Encoding': 'br, gzip' } });  \/\/ backend (Fastify) fastify.register(require('@fastify\/compress'), {   brotliOptions: { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 5 } } });<\/code><\/pre>\n<p> Brotli \u043d\u0430 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 payload \u0434\u0430\u0451\u0442 \u0434\u043e 25 % \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0442\u0440\u0430\u0444\u0438\u043a\u0430 \u0441\u0432\u0435\u0440\u0445 Gzip.<\/p>\n<h3>Await-\u0432-\u0446\u0438\u043a\u043b\u0435<\/h3>\n<p>\u0415\u0441\u0442\u044c 100 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u043e \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u2248 100 \u00d7 \u043e\u0434\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430:<\/p>\n<pre><code class=\"javascript\">for (const id of ids) {   const res = await fetch(`\/api\/item\/${id}`);   items.push(await res.json()); }<\/code><\/pre>\n<p>\u0412\u044b \u043b\u0438\u043d\u0435\u0430\u0440\u0438\u0437\u0443\u0435\u0442\u0435 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">const concurrency = 10;        \/\/ \u0431\u0435\u0440\u0435\u0436\u0451\u043c \u0431\u044d\u043a\u0435\u043d\u0434 const queue = [...ids]; const results = [];  await Promise.all(   Array.from({ length: concurrency }, async function worker() {     while (queue.length) {       const id = queue.pop();       const res = await fetch(`\/api\/item\/${id}`);       results.push(await res.json());     }   }) );<\/code><\/pre>\n<h3>\u041c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442: fetch vs undici.request<\/h3>\n<p>\u0414\u0430\u0436\u0435 \u043f\u0440\u0438 \u0432\u0441\u0435\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u0445\u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b.<\/p>\n<p>Node-fetch \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u00ab\u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u043f\u043e\u0432\u0435\u0440\u0445 \u043e\u0431\u0451\u0440\u0442\u043a\u0438\u00bb: web-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c &gt; \u043b\u0438\u0448\u043d\u0438\u0439 GC, \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. Matt Pocock \u0438\u0437\u043c\u0435\u0440\u0438\u043b: <code>undici.request<\/code> \u2248 2\u20133\u00d7 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 built-in <code>fetch<\/code>.<\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<pre><code class=\"javascript\">import { request } from 'undici';  const { body } = await request('https:\/\/inventory.local\/items', {   method: 'POST',   body: JSON.stringify(payload),   headers: { 'content-type': 'application\/json' }, }); const data = await body.json();        \/\/ \u0432\u0441\u0451 \u0435\u0449\u0451 \u0443\u0434\u043e\u0431\u043d\u043e<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0412\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c HTTP-\u0432\u044b\u0437\u043e\u0432\u044b, \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043d\u0430 <code>undici.request<\/code> \u0434\u0430\u0441\u0442 3\u00d7 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435.<\/p>\n<h3> CORS-preflight<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043a \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u043c\u0443 API \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u0442 +150\u2013250 \u043c\u0441. \u0412 Perf-\u0442\u0430\u0431 \u00ab\u0446\u0430\u0440\u0438\u0442\u00bb \u043b\u0438\u0448\u043d\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 <code>OPTIONS<\/code>, \u0430 \u0441\u043b\u0435\u0434\u043e\u043c \u0443\u0436\u0435 \u0438\u0434\u0451\u0442 \u0412\u0430\u0448 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 <code>GET\/POST<\/code>.<\/p>\n<p>\u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 <strong>preflight-\u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043f\u0440\u0438 \u043d\u0435\u0441-simple \u043c\u0435\u0442\u043e\u0434\u0430\u0445\/\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445 (\u043b\u044e\u0431\u043e\u0435 <code>Content-Type<\/code> \u043a\u0440\u043e\u043c\u0435 <code>application\/x-www-form-urlencoded<\/code>, \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 headers, <code>credentials: true<\/code> \u0438 \u0442.\u043f.). \u041f\u043e\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u043e\u0442\u0432\u0435\u0442\u0438\u0442 200 OK, \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u2014 \u0434\u0432\u043e\u0439\u043d\u043e\u0439 RTT \u043f\u043b\u044e\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434\u0435. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<ol>\n<li>\n<p><strong>\u0423\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441<\/strong>: <code>GET<\/code> \u0438\u043b\u0438 <code>POST<\/code> \u0441 <code>Content-Type: text\/plain<\/code> (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043e\u043a) \u0438 \u0431\u0435\u0437 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u044d\u0448\u0438\u0440\u0443\u0439\u0442\u0435 preflight<\/strong> \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"javascript\">\/\/ Express + cors app.options('*', cors({   origin: true,   credentials: true,   maxAge: 86_400,      \/\/ 24 \u0447 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0430\u0442\u044c OPTIONS }));<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u0421\u0445\u043b\u043e\u043f\u043d\u0438\u0442\u0435 \u0434\u043e\u043c\u0435\u043d\u044b<\/strong> \u2014 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0438\u0442\u0435 API \u043d\u0430 \u043f\u043e\u0434\u043f\u0443\u0442\u044c (<code>\/api<\/code>) \u0442\u043e\u0433\u043e \u0436\u0435 origin, \u0447\u0442\u043e\u0431\u044b CORS \u0438\u0441\u0447\u0435\u0437 \u0432\u043e\u0432\u0441\u0435.<\/p>\n<\/li>\n<\/ol>\n<h3>HOL-\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0432 HTTP\/2: \u043e\u0434\u0438\u043d \u0436\u0438\u0440\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u0432\u0441\u0435\u0445<\/h3>\n<p>\u041d\u0430 HTTP\/2-\u0431\u044d\u043a\u0435\u043d\u0434\u0435 \u0432\u0441\u043f\u043b\u0435\u0441\u043a\u0438  \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0440\u043e\u0432\u043d\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u0437\u0430\u0433\u0440\u0443\u0437\u043e\u043a\/\u043e\u0442\u0434\u0430\u0447 (\u0444\u0430\u0439\u043b\u044b &gt; 5 \u041c\u0411). \u041b\u043e\u0433\u0438 \u043f\u0443\u0441\u0442\u044b, CPU \u2014 \u043d\u043e\u0440\u043c, \u0441\u0435\u0442\u044c \u2014 \u043d\u043e\u0440\u043c, \u043d\u043e \u0444\u0440\u043e\u043d\u0442 \u0444\u0440\u0438\u0437\u0438\u0442.<\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0442\u0435\u0440\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 <strong>TCP-\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442<\/strong> \u0432\u0441\u0451, \u0447\u0442\u043e \u0438\u0434\u0451\u0442 \u043f\u043e\u0441\u043b\u0435 \u0433\u043e\u043b\u043e\u0432\u044b. \u0412 HTTP\/2 \u0432\u0441\u0435 \u0441\u0442\u0440\u0438\u043c\u044b \u043c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 <strong>\u043e\u0434\u0438\u043d TCP-\u043f\u043e\u0442\u043e\u043a<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0430\u0434\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 \u043d\u0430 file-upload \u0441\u0442\u043e\u043f\u043e\u0440\u0438\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 API-\u0432\u044b\u0437\u043e\u0432\u044b. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<p><strong>\u0420\u0430\u0437\u043d\u0435\u0441\u0438\u0442\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0435 \u0438 \u043c\u0435\u043b\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b<\/strong>: \u0434\u0435\u0440\u0436\u0438\u0442\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0433\u0435\u043d\u0442\/\u0434\u043e\u043c\u0435\u043d\/\u043f\u043e\u0440\u0442 \u043f\u043e\u0434 \u0442\u043e\u043b\u0441\u0442\u044b\u0435 \u0430\u043f\u043b\u043e\u0443\u0434\u044b.<\/p>\n<pre><code class=\"json\">const bulkAgent = new Agent({ maxStreamsPerConnection: 1 }); \/\/ \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 await fetch(largeUrl, { dispatcher: bulkAgent, body: bigFile });<\/code><\/pre>\n<p><strong>\u0412\u043a\u043b\u044e\u0447\u0438\u0442\u0435 HTTP\/3 (QUIC)<\/strong> \u2014 \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e UDP, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c. <strong>\u0422\u044e\u043d\u044c\u0442\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0438\u0437\u0430\u0446\u0438\u044e<\/strong>: <code>http2_max_concurrent_streams<\/code>, <code>priority<\/code>-\u0445\u0438\u043d\u0442\u044b \u0438\u043b\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 WAF-\u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u0430\u043a\u0435\u0442-\u043f\u043e\u0442\u0435\u0440\u044f \u043d\u0435 \u043a\u043b\u0430\u043b\u0430 \u0432\u0441\u0451 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u0441\u0442\u0435\u043d\u0434\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 upload-\u0440\u0443\u0447\u0435\u043a \u043d\u0430 HTTP\/3 \u0434\u0430\u043b\u043e \u201345 % \u043a \u043c\u0435\u0434\u0438\u0430\u043d\u0435 \u0438 \u201379 % \u043a p99.<\/p>\n<h3>JSON.stringify \u043f\u0435\u0440\u0435\u0434 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u043e\u0439<\/h3>\n<p>\u041f\u0440\u0438 \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u043c <code>POST \/bulk<\/code> \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0435 100 % CPU, \u0430 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0442\u0440\u0430\u0444\u0438\u043a \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043f\u043e\u0437\u0434\u043d\u043e: \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043a\u0438\u043f\u0438\u0442 \u0435\u0449\u0451 \u0434\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e <code>fetch()<\/code>.<\/p>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u043d\u0435 \u0443\u043b\u0435\u0442\u0438\u0442, \u043f\u043e\u043a\u0430 \u0412\u044b \u043d\u0435 <strong>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0435<\/strong> payload. \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0432\u044f\u0437\u043a\u0430<\/p>\n<pre><code class=\"javascript\">await fetch(url, { body: JSON.stringify(hugeObject) });<\/code><\/pre>\n<p>\u043e\u0434\u043d\u0438\u043c \u043c\u0430\u0445\u043e\u043c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u043c\u0435\u0433\u0430\u0431\u0430\u0439\u0442 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044f event-loop. \u0410 \u0435\u0441\u043b\u0438 \u0412\u044b \u0435\u0449\u0451 \u0438 \u043a\u043b\u043e\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u0434\u0430\u0432 <code>body: {...data}<\/code>, \u0432\u0440\u0435\u043c\u044f \u0443\u0434\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u0412 Node 24\/Chromium 122 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d <code><strong>structuredClone()<\/strong><\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 deep-copy, \u043d\u043e \u043e\u043d \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0435\u043d. <\/p>\n<h4>\u0424\u0438\u043a\u0441<\/h4>\n<ol>\n<li>\n<p><strong>\u0421\u0442\u0440\u0438\u043c\u043e\u0432\u044b\u0439 multipart\/NDJSON-upload<\/strong> \u2014 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u0443\u0441\u043e\u0447\u043d\u043e:<\/p>\n<pre><code class=\"javascript\">import { Readable } from 'node:stream'; const enc = new TextEncoder(); const stream = Readable.from(   bigArray.map(x =&gt; enc.encode(JSON.stringify(x) + '\\n')) ); await fetch('\/bulk\/ingest', { method: 'POST', body: stream });<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434 \u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e &lt; 50 \u041c\u0411 \u0434\u0430\u0436\u0435 \u043d\u0430 1 \u0413\u0411 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0438\u0448\u0438\u0442\u0435 \u0432 BSON\/MsgPack<\/strong>, \u043c\u0438\u043d\u0443\u044f stringify.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043e\u0442 \u043b\u0438\u0448\u043d\u0435\u0433\u043e deep-copy<\/strong> \u2014 \u0432\u043c\u0435\u0441\u0442\u043e<\/p>\n<pre><code class=\"javascript\">const safe = structuredClone(huge);<\/code><\/pre>\n<p>\u0447\u0430\u0449\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Schemas.<\/p>\n<\/li>\n<\/ol>\n<h3>\u0418\u0442\u0430\u043a<\/h3>\n<p><strong>\u0418\u0437\u043c\u0435\u0440\u044f\u0439\u0442\u0435<\/strong>. <code>npx bench-rest<\/code>, <code>autocannon<\/code>, \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043d\u044b\u0439 PerformanceTab. <strong>\u0427\u0438\u043d\u0438\u0442\u0435 \u043f\u043e \u0441\u043b\u043e\u044f\u043c<\/strong>: \u0441\u0435\u0442\u044c &gt; \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b &gt; \u043a\u043b\u0438\u0435\u043d\u0442 &gt; \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f &gt; \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. <strong>\u041d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432<\/strong>: GraphQL-over-HTTP\/2, gRPC-web, msgpack \u0432\u043c\u0435\u0441\u0442\u043e JSON. <strong>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0439\u0442\u0435<\/strong>. \u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 Artillery \u0432 CI, \u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0430\u043b\u0435\u0440\u0442\u044b \u043d\u0430 p95&gt;200 \u043c\u0441.<\/p>\n<hr\/>\n<p>\u0412\u0441\u0435\u0445, \u043a\u0442\u043e \u0438\u0449\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0443\u0442\u044c \u043a \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u044b\u043c \u0437\u043d\u0430\u043d\u0438\u044f\u043c, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 \u0441\u0435\u0440\u0438\u044e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 <a href=\"https:\/\/otus.pw\/Y7TK\/\">\u043a\u0443\u0440\u0441\u0430 Fullstack Developer<\/a>:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/mUPPe\/\">Web-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0438 Shadow DOM: \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 UI\u2011\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/a>\u00a0\u2014 <em>28\u00a0\u043c\u0430\u044f<\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/uXTg\/\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u00a0API \u0434\u043b\u044f\u00a0\u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445: \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u00a0\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443<\/a>\u00a0\u2014 <em>3\u00a0\u0438\u044e\u043d\u044f <\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/rrGwH\/\">\u041c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441\u00a0HTML \u0438 CSS \u0441\u00a0\u043f\u043e\u043c\u043e\u0449\u044c\u044e JavaScript<\/a>\u00a0\u2014 <em>12\u00a0\u0438\u044e\u043d\u044f<\/em><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/otus.pw\/Y9aL\/\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441\u00a0\u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 html \u0442\u0435\u0433\u0430\u043c\u0438 \u0438 \u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c\u0438<\/a>\u00a0\u2014 <em>17\u00a0\u0438\u044e\u043d\u044f<\/em><\/p>\n<\/li>\n<\/ul>\n<\/div>\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\/articles\/912854\/\"> https:\/\/habr.com\/ru\/articles\/912854\/<\/a><br \/><\/br><\/br><\/p>\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-461220","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/461220","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=461220"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/461220\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}