Skip to content

Commit

Permalink
Avoid using deprecated Buffer constructor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ChALkeR committed Feb 22, 2018
1 parent a321fd8 commit 2d90739
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion clone.js
Expand Up @@ -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)) {
Expand Down

0 comments on commit 2d90739

Please sign in to comment.