From: Feross Aboukhadijeh Date: Thu, 9 Feb 2017 03:38:10 +0000 (-0800) Subject: Replace `Buffer.isBuffer` with `instanceof Buffer` X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=9e12b1d89b2fe67faf91eb93087bafd0975699ec;p=buffer.git Replace `Buffer.isBuffer` with `instanceof Buffer` It's one less function call, and we don't need to avoid `instanceof Buffer` anymore because our Buffer is proper subclass of Uint8Array now, instead of just an Uint8Array with the methods added on as properties. --- diff --git a/index.js b/index.js index 724d6f2..3bef583 100644 --- a/index.js +++ b/index.js @@ -239,7 +239,7 @@ function fromArrayBuffer (array, byteOffset, length) { } function fromObject (obj) { - if (Buffer.isBuffer(obj)) { + if (obj instanceof Buffer) { var len = checked(obj.length) | 0 var buf = createBuffer(len) @@ -285,11 +285,11 @@ function SlowBuffer (length) { } Buffer.isBuffer = function isBuffer (b) { - return !!(b != null && b._isBuffer) + return b instanceof Buffer } Buffer.compare = function compare (a, b) { - if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + if (!(a instanceof Buffer) || !(b instanceof Buffer)) { throw new TypeError('Arguments must be Buffers') } @@ -351,7 +351,7 @@ Buffer.concat = function concat (list, length) { var pos = 0 for (i = 0; i < list.length; ++i) { var buf = list[i] - if (!Buffer.isBuffer(buf)) { + if (!(buf instanceof Buffer)) { throw new TypeError('"list" argument must be an Array of Buffers') } buf.copy(buffer, pos) @@ -361,7 +361,7 @@ Buffer.concat = function concat (list, length) { } function byteLength (string, encoding) { - if (Buffer.isBuffer(string)) { + if (string instanceof Buffer) { return string.length } if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer) { @@ -474,8 +474,8 @@ function slowToString (encoding, start, end) { } } -// The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect -// Buffer instances. +// The property is used by the `is-buffer` npm package to detect Buffer instances +// in Safari 5-7. Remove this eventually. Buffer.prototype._isBuffer = true function swap (b, n, m) { @@ -529,7 +529,7 @@ Buffer.prototype.toString = function toString () { } Buffer.prototype.equals = function equals (b) { - if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (!(b instanceof Buffer)) throw new TypeError('Argument must be a Buffer') if (this === b) return true return Buffer.compare(this, b) === 0 } @@ -545,7 +545,7 @@ Buffer.prototype.inspect = function inspect () { } Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { - if (!Buffer.isBuffer(target)) { + if (!(target instanceof Buffer)) { throw new TypeError('Argument must be a Buffer') } @@ -647,7 +647,7 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { } // Finally, search either indexOf (if dir is true) or lastIndexOf - if (Buffer.isBuffer(val)) { + if (val instanceof Buffer) { // Special case: looking for empty string/buffer always fails if (val.length === 0) { return -1 @@ -1213,7 +1213,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { } function checkInt (buf, value, offset, ext, max, min) { - if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') + if (!(buf instanceof Buffer)) throw new TypeError('"buffer" argument must be a Buffer instance') if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') if (offset + ext > buf.length) throw new RangeError('Index out of range') } @@ -1541,7 +1541,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { this[i] = val } } else { - var bytes = Buffer.isBuffer(val) + var bytes = val instanceof Buffer ? val : new Buffer(val, encoding) var len = bytes.length