Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6feea08

Browse filesBrowse files
alemuresItalo A. Casas
authored andcommitted
buffer: preallocate array with buffer length
Because the final array length is known, it's better to allocate its final length at initialization time to avoid future reallocations. Also add 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') PR-URL: #11733 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent a703bde commit 6feea08
Copy full SHA for 6feea08

File tree

Expand file treeCollapse file tree

1 file changed

+2
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+2
-2
lines changed
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,8 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
799799

800800

801801
Buffer.prototype.toJSON = function() {
802-
if (this.length) {
803-
const data = [];
802+
if (this.length > 0) {
803+
const data = new Array(this.length);
804804
for (var i = 0; i < this.length; ++i)
805805
data[i] = this[i];
806806
return { type: 'Buffer', data };

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.