From 2d907392855214439ee9f9b6c60b3e47c5fae07b 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: Thu, 22 Feb 2018 23:24:42 +0300 Subject: [PATCH] Avoid using deprecated Buffer constructor Use Buffer.allocUnsafe directly on Node.js >= 4.5.0 This usecase doesn't need zero-filling, as is properly filled with the parent buffer. This behaves exactly the same as Buffer(number) on 4.x and 6.x, and slightly faster that Buffer(number) on 8.x and above, as doesn't perform zero-fill. Older Node.js versions (<4.5.0) use the old code path. Refs: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor --- clone.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clone.js b/clone.js index 80d0c76..3fa5fad 100644 --- a/clone.js +++ b/clone.js @@ -104,7 +104,13 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) { } else if (clone.__isDate(parent)) { child = new Date(parent.getTime()); } else if (useBuffer && Buffer.isBuffer(parent)) { - child = new Buffer(parent.length); + if (Buffer.allocUnsafe) { + // Node.js >= 4.5.0 + child = Buffer.allocUnsafe(parent.length); + } else { + // Older Node.js versions + child = new Buffer(parent.length); + } parent.copy(child); return child; } else if (_instanceof(parent, Error)) {