Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Preallocate array with buffer length
subsystem: buffer

Because the final array length is known, it's better to allocate its
final length at initialization time to avoid future reallocations.

It also adds an explicit buffer length greater than 0 comparison so
it's more readable, avoids the internal ToBoolean call and follows the
standard Node.js API format (as it can be checked in other similar
structures where 'length > 0' is preferred over 'length')
  • Loading branch information
alemures committed Mar 13, 2017
commit 2fd45da1fce902a105327c2c591f411a524e932a
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ Buffer.prototype.write = function(string, offset, length, encoding) {


Buffer.prototype.toJSON = function() {
if (this.length) {
const data = [];
if (this.length > 0) {
const data = new Array(this.length);
for (var i = 0; i < this.length; ++i)
data[i] = this[i];
return { type: 'Buffer', data };
Expand Down