Exstream in depth
Exstream as a chain of streams
Consider this example:
js
const _ = require('exstream.js')
const sourceStream = _([1,2,3])
const multipliedStream = sourceExstream.map(x => x * 2)
const filteredStream = multipliedExstream.filter(x => x < 6)
const res = filteredExstream.values()
// res is [2, 4]
The main concept that you should keep in mind to understand how exstream works, is that an exstream chain is actually a chain of streams:
sourceStream
is equivalent to aReadable
Node.js stream.multipliedStream
is equivalent to aTransform
stream that usessourceStream
as sourcefilteredStream
is equivalent to aTransform
stream that usesmultipliedStream
as source- Calling
.values()
is equivalent to pipefilteredStream
in aWritable
stream that pushes the values emitted byfilteredStream
in an array
This code is functionally equivalent to this:
js
const { Readable, Transform, Writable } = require('stream')
function * sourceIterator(arr) {
for(const x of arr) yield arr
}
const sourceStream = Readable.from(sourceIterator([1,2,3]))
const multiplyBy2 = new Transform({
objectMode: true,
transform: function (chunk, enc, cb) {
this.push(chunk * 2)
cb()
}
})
const filterIfLt6 = new Transform({
objectMode: true,
transform: function (chunk, enc, cb) {
if(chunk < 6) this.push(chunk)
cb()
}
})
const res = []
const writeValues = new Writable({
objectMode: true,
write (chunk, enc, cb) {
res.push(rec)
cb()
}
})
sourceStream
.pipe(multiplyBy2)
.pipe(filterIfLt6)
.pipe(writeValues)
.on('finish', () => {
console.log(res)
// res is [2, 4]
})
console.log(res)
//res is empty!
The main differences are 2:
- The exstream version is way more compact and readable that the plain node.js version
- If the source and the transformation chain are synchronous, the computation is synchronous as well, while with standard Node.js streams you have to listen to the finish event in order to have
res
populated. So, exstream is async only when needed. This let exstream achieve superior speed in many use cases compared with a plain Node.js stream implementation.
Back-Pressure
TODO
Performance considerations
TODO