From f197e7a2a52d957db7574a8fe6296d9dc01685c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Fri, 27 Jul 2018 01:14:25 +0300 Subject: [PATCH] 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. --- lib/buffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index 061ff7ebe440da..7902fc6fde5dcc 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -276,7 +276,7 @@ function assertSize(size) { */ Buffer.alloc = function alloc(size, fill, encoding) { assertSize(size); - if (fill !== undefined && size > 0) { + if (fill !== undefined && fill !== 0 && size > 0) { return _fill(createUnsafeBuffer(size), fill, encoding); } return new FastBuffer(size);