What is the correct usage of _writev()
in node.js?
The documentation says:
If a stream implementation is capable of processing multiple chunks of data at once, the
writable._writev()
method should be implemented.
It also says:
The primary intent of
writable.cork()
is to avoid a situation where writing many small chunks of data to a stream do not cause a backup in the internal buffer that would have an adverse impact on performance. In such situations, implementations that implement thewritable._writev()
method can perform buffered writes in a more optimized manner.
From a stream implementation perspective this is okay. But from a writable stream consumer perspective, the only way that write
or writev
gets invoked is through Writable.write()
and writable.cork()
I would like to see a small example which would depict the practical use case of implementing _writev()
A
writev
method can be added to the instance, in addition towrite
, and if the streams contains several chunks that method will be picked instead ofwrite
. For example Elasticsearch allows you to bulk insert records; so if you are creating a Writable stream to wrap Elasticsearch, it makes sense to have awritev
method doing a single bulk insert rather than several individual ones, it is far more efficient. The same holds true, for example, for MongoDB and so on.This post (not mine) shows an Elasticsearch implementation https://medium.com/@mark.birbeck/using-writev-to-create-a-fast-writable-stream-for-elasticsearch-ac69bd010802