Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 35bca31

Browse filesBrowse files
mscdexBridgeAR
authored andcommitted
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>
1 parent a123a20 commit 35bca31
Copy full SHA for 35bca31

File tree

Expand file treeCollapse file tree

3 files changed

+28
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-1
lines changed
Open diff view settings
Collapse file

‎benchmark/buffers/buffer-equals.js‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
size: [0, 512, 16386],
6+
difflen: ['true', 'false'],
7+
n: [1e6]
8+
});
9+
10+
function main({ n, size, difflen }) {
11+
const b0 = Buffer.alloc(size, 'a');
12+
const b1 = Buffer.alloc(size + (difflen === 'true' ? 1 : 0), 'a');
13+
14+
if (b1.length > 0)
15+
b1[b1.length - 1] = 'b'.charCodeAt(0);
16+
17+
bench.start();
18+
for (let i = 0; i < n; i++) {
19+
b0.equals(b1);
20+
}
21+
bench.end(n);
22+
}
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,14 @@ Buffer.prototype.equals = function equals(otherBuffer) {
716716
throw new ERR_INVALID_ARG_TYPE(
717717
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
718718
}
719+
719720
if (this === otherBuffer)
720721
return true;
721722

722-
return _compare(this, otherBuffer) === 0;
723+
if (this.byteLength !== otherBuffer.byteLength)
724+
return false;
725+
726+
return this.byteLength === 0 || _compare(this, otherBuffer) === 0;
723727
};
724728

725729
let INSPECT_MAX_BYTES = 50;
Collapse file

‎test/benchmark/test-benchmark-buffer.js‎

Copy file name to clipboardExpand all lines: test/benchmark/test-benchmark-buffer.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ runBenchmark('buffers',
1212
'bytes=0',
1313
'byteLength=1',
1414
'charsPerLine=6',
15+
'difflen=false',
1516
'encoding=utf8',
1617
'endian=BE',
1718
'len=256',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.