Skip to content
Permalink
Browse files

buffer: improve equals() performance

PR-URL: #29199
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information...
mscdex authored and BridgeAR committed Aug 19, 2019
1 parent a123a20 commit 35bca312edb1caf0d012cd116bf9db7a2d1dfd50
Showing with 28 additions and 1 deletion.
  1. +22 −0 benchmark/buffers/buffer-equals.js
  2. +5 −1 lib/buffer.js
  3. +1 −0 test/benchmark/test-benchmark-buffer.js
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common.js');

const bench = common.createBenchmark(main, {
size: [0, 512, 16386],
difflen: ['true', 'false'],
n: [1e6]
});

function main({ n, size, difflen }) {
const b0 = Buffer.alloc(size, 'a');
const b1 = Buffer.alloc(size + (difflen === 'true' ? 1 : 0), 'a');

if (b1.length > 0)
b1[b1.length - 1] = 'b'.charCodeAt(0);

bench.start();
for (let i = 0; i < n; i++) {
b0.equals(b1);
}
bench.end(n);
}
@@ -716,10 +716,14 @@ Buffer.prototype.equals = function equals(otherBuffer) {
throw new ERR_INVALID_ARG_TYPE(
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
}

if (this === otherBuffer)
return true;

return _compare(this, otherBuffer) === 0;
if (this.byteLength !== otherBuffer.byteLength)
return false;

return this.byteLength === 0 || _compare(this, otherBuffer) === 0;
};

let INSPECT_MAX_BYTES = 50;
@@ -12,6 +12,7 @@ runBenchmark('buffers',
'bytes=0',
'byteLength=1',
'charsPerLine=6',
'difflen=false',
'encoding=utf8',
'endian=BE',
'len=256',

0 comments on commit 35bca31

Please sign in to comment.
You can’t perform that action at this time.