From 6622ac798d2624eb434b9b51c34a8dc2f9b07149 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/21989 Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat Reviewed-By: Minwoo Jung Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Gireesh Punathil Reviewed-By: Tobias Nießen --- 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);