Created
July 31, 2024 12:16
-
-
Save jsumners/17587fe1b0fb021436642567e2d1d402 to your computer and use it in GitHub Desktop.
Testing stream data collection methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
const MAX_READ = 104_857_600 | |
let start = process.hrtime.bigint() | |
await plusEquals() | |
let end = process.hrtime.bigint() | |
const peTime = end - start | |
console.log('plusEquals => ', peTime) | |
start = process.hrtime.bigint() | |
await concat() | |
end = process.hrtime.bigint() | |
const cTime = end - start | |
console.log('concat => ', cTime) | |
if (cTime < peTime) { | |
console.log('concat is faster') | |
} else if (peTime < cTime) { | |
console.log('plusEquals is faster') | |
} else { | |
console.log('both equal') | |
} | |
async function plusEquals() { | |
const { promise, resolve } = Promise.withResolvers() | |
const fd = fs.createReadStream('/dev/urandom') | |
let total = 0 | |
let data | |
fd.on('data', d => { | |
data += d | |
total += d.byteLength | |
if (total >= MAX_READ) { | |
fd.destroy() | |
console.log(typeof data, Buffer.isBuffer(data)) | |
resolve() | |
} | |
}) | |
await promise | |
} | |
async function concat() { | |
const { promise, resolve } = Promise.withResolvers() | |
const fd = fs.createReadStream('/dev/urandom') | |
let total = 0 | |
let data = Buffer.alloc(0) | |
fd.on('data', d => { | |
data = Buffer.concat([data, d]) | |
total += d.byteLength | |
if (total >= MAX_READ) { | |
fd.destroy() | |
console.log(typeof data, Buffer.isBuffer(data)) | |
resolve() | |
} | |
}) | |
await promise | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment