I don't understand what is the purpose of the Buffer.isBuffer
function when instanceof
works like a charm :
var b = new Buffer('blabla') assert.ok(b instanceof Buffer)
The Buffer. copy() method simply copies all the values from input buffer to another buffer.
Buffers in Streams Streams work on a concept called buffer. A buffer is a temporary memory that a stream takes to hold some data until it is consumed. In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes.
Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. Buffer class is a global class that can be accessed in an application without importing the buffer module.
A Buffer is a temporary memory that holds data until it is consumed. A Buffer represents a fixed-size chunk of memory allocated outside of the V8 JavaScript engine. You can think of a Buffer like an array of integers that each represent a byte of data. The Buffer.
Well, actually these are the same (currently at least):
-- lib/buffer.js:
Buffer.isBuffer = function isBuffer(b) { return util.isBuffer(b); };
-- lib/util.js:
function isBuffer(arg) { return arg instanceof Buffer; } exports.isBuffer = isBuffer;
... so the only possible reason is readability. Note that before this specific implementation there was a set of macros for type checks, used when building the source. But it has been changed with this commit, and that was the reasoning:
Adding macros to Node's JS layer increases the barrier to contributions, and it breaks programs that export Node's js files for userland modules. (For example, several browserify transforms, my readable-streams polyfill, the util-debuglog module, etc.) These are not small problems.
I'd suggest checking the whole discussion in the commit's pull request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With