Skip to content

Commit 2757712

Browse files
committed
buffer: always use _copy for copy
This fixes a performance regression for Buffer.copy(target, 0) and brings it back inline with Buffer.write. V8 has a massive TypedArray.prototype.set penalty on SharedArrayBuffer Buffer.set and Buffer.copy are up to 8.4x slower when writing to a SharedArrayBuffer vs a regular ArrayBuffer, while Buffer.write (string encoding) is completely unaffected. 256 bytes, varying offset (Apple M3 Pro, Node 25.6.1): ArrayBuffer SharedArrayBuffer Slowdown Buffer.set 13.6 ns 56.1 ns 4.1x Buffer.copy 17.0 ns 65.1 ns 3.8x Buffer.write 75.8 ns 74.1 ns 1.0x (unaffected) 4096 bytes, varying offset: ArrayBuffer SharedArrayBuffer Slowdown Buffer.set 80.3 ns 674.2 ns 8.4x Buffer.copy 78.4 ns 677.7 ns 8.6x Buffer.write 190.6 ns 186.1 ns 1.0x (unaffected)
1 parent 2030fd3 commit 2757712

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

lib/buffer.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ function _copyActual(source, target, targetStart, sourceStart, sourceEnd, isUint
251251
if (nb <= 0)
252252
return 0;
253253

254-
if (sourceStart === 0 && nb === sourceLen && (isUint8Copy || isUint8Array(target))) {
255-
TypedArrayPrototypeSet(target, source, targetStart);
256-
} else {
257-
_copy(source, target, targetStart, sourceStart, nb);
258-
}
254+
_copy(source, target, targetStart, sourceStart, nb);
259255

260256
return nb;
261257
}

0 commit comments

Comments
 (0)