From: Feross Aboukhadijeh Date: Tue, 10 Feb 2015 02:21:08 +0000 (-0800) Subject: don't compare same buffers X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=ffccb1a5daf3c4bca0bf8cba66d7947bd2805767;p=buffer.git don't compare same buffers Copied from iojs: https://github.com/iojs/io.js/pull/742 --- diff --git a/index.js b/index.js index 78f74f2..21c8a09 100644 --- a/index.js +++ b/index.js @@ -149,6 +149,8 @@ Buffer.compare = function (a, b) { if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) throw new TypeError('Arguments must be Buffers') + if (a === b) return 0 + var x = a.length var y = b.length for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} @@ -289,6 +291,7 @@ Buffer.prototype.toString = function (encoding, start, end) { Buffer.prototype.equals = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true return Buffer.compare(this, b) === 0 } @@ -305,6 +308,7 @@ Buffer.prototype.inspect = function () { Buffer.prototype.compare = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return 0 return Buffer.compare(this, b) }