From 32d65c001d6d3e723e9dc2bd9584a685ff093e5b Mon Sep 17 00:00:00 2001 From: kylo5aby <109658203+zhenweijin@users.noreply.github.com> Date: Fri, 10 Nov 2023 17:45:14 +0800 Subject: [PATCH] buffer: improve Buffer.equals performance PR-URL: https://github.com/nodejs/node/pull/50621 Refs: https://github.com/nodejs/node/issues/50620 Reviewed-By: Antoine du Hamel Reviewed-By: Yagiz Nizipli Reviewed-By: Debadree Chatterjee Reviewed-By: Darshan Sen --- lib/buffer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index ee30c69cc9611c..ee8b87a191ddbd 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -860,11 +860,11 @@ Buffer.prototype.equals = function equals(otherBuffer) { if (this === otherBuffer) return true; - - if (this.byteLength !== otherBuffer.byteLength) + const len = TypedArrayPrototypeGetByteLength(this); + if (len !== TypedArrayPrototypeGetByteLength(otherBuffer)) return false; - return this.byteLength === 0 || _compare(this, otherBuffer) === 0; + return len === 0 || _compare(this, otherBuffer) === 0; }; let INSPECT_MAX_BYTES = 50;