Skip to content

Commit 2d90739

Browse files
authored
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
1 parent a321fd8 commit 2d90739

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎clone.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
104104
} else if (clone.__isDate(parent)) {
105105
child = new Date(parent.getTime());
106106
} else if (useBuffer && Buffer.isBuffer(parent)) {
107-
child = new Buffer(parent.length);
107+
if (Buffer.allocUnsafe) {
108+
// Node.js >= 4.5.0
109+
child = Buffer.allocUnsafe(parent.length);
110+
} else {
111+
// Older Node.js versions
112+
child = new Buffer(parent.length);
113+
}
108114
parent.copy(child);
109115
return child;
110116
} else if (_instanceof(parent, Error)) {

0 commit comments

Comments
 (0)