Skip to content

Instantly share code, notes, and snippets.

@Jarred-Sumner
Created May 2, 2024 11:11
Show Gist options
  • Save Jarred-Sumner/bc5fc8e092f2036c396e8fb507a9f3c2 to your computer and use it in GitHub Desktop.
Save Jarred-Sumner/bc5fc8e092f2036c396e8fb507a9f3c2 to your computer and use it in GitHub Desktop.
const server = process.argv.at(-1);
const fmt = new Intl.NumberFormat();
let total = 0;
const batch = 50;
const delay = 0;
while (true) {
const array = new Array(batch);
for (let i = 0; i < batch; i++) {
array[i] = fetch(server);
}
await Promise.all(array);
await new Promise((r) => setTimeout(r, delay));
console.log(
"RSS",
(process.memoryUsage.rss() / 1024 / 1024) | 0,
"MB after",
fmt.format((total += batch)) + " fetch() requests"
);
}
const blob = new Blob([require("crypto").randomFillSync( new Uint8Array(1024 * 512))]);
Bun.serve({
fetch(req) {
return new Response(blob);
},
port: process.argv.at(-1),
});
@billywhizz
Copy link

this code will work on deno too. seems to perform pretty much the same as bun, but with some strange occasional stalls on my setup. defo see the same issue as you with node.js versions 20 and 21. memory keeps growing and never gets released, even if i force gc on every iteration of the loop.

import * as process from 'node:process'

const server = (globalThis.Deno? Deno.args : process.argv).at(-1);

const fmt = new Intl.NumberFormat();
let total = 0;
const batch = 50;
const delay = 0;
while (true) {
  const array = new Array(batch);
  for (let i = 0; i < batch; i++) {
    array[i] = fetch(server);
  }
  await Promise.all(array);
  await new Promise((r) => setTimeout(r, delay));
  console.log(
    "RSS",
    (process.memoryUsage.rss() / 1024 / 1024) | 0,
    "MB after",
    fmt.format((total += batch)) + " fetch() requests"
  );
}

@billywhizz
Copy link

Also, if you change the fetch call to await the body of the response, then bun/deno/node20/21 all behave pretty much the same but node.js uses ~30% less memory (150 MB) than deno or bun (both around 210 MB) on ubuntu 22.04.

    array[i] = fetch(server).then(res => res.text());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment