Reputation: 508
Why do we need Buffer.isBuffer
method when we know it's doing the same thing as instanceof Buffer
?
https://github.com/nodejs/node/blob/master/lib/buffer.js#L306
Upvotes: 0
Views: 419
Reputation: 123473
It's not technically necessary. It exists for convenience and is probably, to at least a degree, idiomatic.
No extended explanation was given when it was defined, but it appears to have been a refactoring (DRY – answer "what is a buffer" once and reuse) and/or a stylistic preference:
- if (!(buffer instanceof Buffer)) {
+ if (!Buffer.isBuffer(buffer)) {
There was probably some inspiration taken from Array.isArray()
.
And, it does also offer some additional backwards compatibility (or possibly some future-proofing), as not all of Node's versions have had a single type for Buffers:
Buffer.isBuffer = function isBuffer(b) {
return b instanceof Buffer || b instanceof SlowBuffer;
};
Upvotes: 2