Skip to content

Instantly share code, notes, and snippets.

@tamtamchik
Created October 4, 2021 19:33
Show Gist options
  • Save tamtamchik/8ad09447ae9ad8621e268fa9921d5917 to your computer and use it in GitHub Desktop.
Save tamtamchik/8ad09447ae9ad8621e268fa9921d5917 to your computer and use it in GitHub Desktop.
fetch-https.js
import https from 'node:https'
import fetch from 'node-fetch'
const agent = new https.Agent({ keepAlive: true })
const toMb = x => `${(x / 1024 / 1024).toFixed(2)} Mb`
async function start () {
const s = process.memoryUsage()
for (let i = 0; i < 1000; i++) {
const res = await fetch('https://example.com', { agent })
for await (const chunk of res.body) {} // do nothing
}
const e = process.memoryUsage()
for (const k of Object.keys(s)) {
console.log(`memoryUsage.${k}: ${toMb(s[k])} => ${toMb(e[k])} (Diff: ${(e[k]/s[k] * 100 - 100).toFixed(2)}%)` );
}
}
process.on('warning', e => {}) /* console.warn(e.stack)) */
start().catch(e => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment