Created
November 20, 2019 18:58
-
-
Save DullReferenceException/63d46986fd1d832113fcbe5d20f7a330 to your computer and use it in GitHub Desktop.
Streams versus async iterables benchmark
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
const { Readable, Transform } = require('stream'); | |
const { createWriteStream } = require('fs'); | |
const { once } = require('events'); | |
const path = require('path'); | |
const tmp = require('os').tmpdir(); | |
function* iterateThroughNumbers(count = 1000000) { | |
for (let i = 0; i < count; i++) { | |
yield i; | |
} | |
} | |
const sourceStream = () => Readable.from(iterateThroughNumbers()); | |
const transformStream = () => new Transform({ | |
writableObjectMode: true, | |
readableObjectMode: false, | |
transform(item, encoding, cb) { | |
return cb(null, transform(item)); | |
} | |
}); | |
const transform = item => (item * 2).toString(); | |
const writeStream = (name = 'out') => createWriteStream(path.join(tmp, './out')); | |
async function toWritableWithTransformStream() { | |
const pipeline = sourceStream() | |
.pipe(transformStream()) | |
.pipe(writeStream('one')); | |
await once(pipeline, 'close'); | |
} | |
async function toWritableWithIteration() { | |
const writable = writeStream('two'); | |
for await (const item of sourceStream()) { | |
const transformed = transform(item); | |
if (!writable.write(transformed)) { | |
await once(writable, 'drain'); | |
} | |
} | |
writable.end(); | |
await once(writable, 'close'); | |
} | |
async function toArrayWithTransformStream() { | |
const buffer = []; | |
const pipeline = sourceStream() | |
.pipe(transformStream()); | |
pipeline.on('data', item => buffer.push(item)); | |
await once(pipeline, 'end'); | |
return buffer; | |
} | |
async function toArrayWithIteration() { | |
const buffer = []; | |
for await (const item of sourceStream()) { | |
const transformed = transform(item); | |
buffer.push(transformed); | |
} | |
return buffer; | |
} | |
async function noStreamtoArray() { | |
const buffer = []; | |
for await (const item of iterateThroughNumbers()) { | |
const transformed = transform(item); | |
buffer.push(transformed); | |
} | |
return buffer; | |
} | |
(async function() { | |
console.time('stream -> TransformStream -> Writable'); | |
await toWritableWithTransformStream(); | |
console.timeEnd('stream -> TransformStream -> Writable'); | |
console.time('stream -> iteration -> Writable'); | |
await toWritableWithIteration(); | |
console.timeEnd('stream -> iteration -> Writable'); | |
console.time('stream -> TransformStream -> read into Array'); | |
await toArrayWithTransformStream(); | |
console.timeEnd('stream -> TransformStream -> read into Array'); | |
console.time('stream -> iteration -> push into Array'); | |
await toArrayWithIteration(); | |
console.timeEnd('stream -> iteration -> push into Array'); | |
console.time('iterable -> iteration -> push into Array'); | |
await noStreamtoArray(); | |
console.timeEnd('iterable -> iteration -> push into Array'); | |
})(); |
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
stream -> TransformStream -> Writable: 1498.982ms | |
stream -> iteration -> Writable: 1220.460ms | |
stream -> TransformStream -> read into Array: 1184.697ms | |
stream -> iteration -> push into Array: 663.321ms | |
iterable -> iteration -> push into Array: 361.910ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment