I came across the following code looking at the source of ws
(a popular WebSocket implementation for Node.js):
const FastBuffer = Buffer[Symbol.species];
So, how do they use this FastBuffer
? Well, no custom implementations or any added code, they just use it to make Buffer
instances from other buffers:
this._buffers[0] = new FastBuffer(
buf.buffer,
buf.byteOffset + n,
buf.length - n
);
return new FastBuffer(buf.buffer, buf.byteOffset, n);
It is well known that the Buffer
constructors are deprecated, but aside from that I couldn't even find a constructor definition that receives an offset
and a length
like the one that ws
seems to be using.
What is this code doing under the hood to access that constructor? How does this add performance? Is this documented somewhere?