Skip to content

Commit 6622ac7

Browse files
ChALkeRtargos
authored andcommitted
buffer: use FastBuffer when fill is set to 0
A large number of libraries seem to use Buffer.alloc(size, 0) instead of just Buffer.alloc(size). We don't need to follow the "create unsafe buffer and fill it" path (i.e. actually allocate and perform fill) in that situation, that is better handled by Uint8Array constructor. Buffer.alloc(size) and Buffer.alloc(size, 0) are equivalent, so use the same code path. Not performing the zero-fill manually and having the underlying memory allocator do it for us can improve speed and reduce the memory usage for situations where Buffer.alloc(size, 0) is used. PR-URL: #21989 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 0ca831a commit 6622ac7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function assertSize(size) {
276276
*/
277277
Buffer.alloc = function alloc(size, fill, encoding) {
278278
assertSize(size);
279-
if (fill !== undefined && size > 0) {
279+
if (fill !== undefined && fill !== 0 && size > 0) {
280280
return _fill(createUnsafeBuffer(size), fill, encoding);
281281
}
282282
return new FastBuffer(size);

0 commit comments

Comments
 (0)