Skip to content

Commit

Permalink
buffer: little improve for Buffer.concat method
Browse files Browse the repository at this point in the history
When buffer list less than 2, no need to calculate the length.
The change's benchmark result is here:
https://gist.github.com/JacksonTian/2c9e2bdec00018e010e6
It improve 15% ~ 25% speed when list only have one buffer,
to other cases no effect.

PR-URL: nodejs#1437
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
  • Loading branch information
JacksonTian authored and Fishrock123 committed May 19, 2015
1 parent 77f5184 commit fa105ab
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ Buffer.concat = function(list, length) {
if (!Array.isArray(list))
throw new TypeError('list argument must be an Array of Buffers.');

if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];

if (length === undefined) {
length = 0;
for (var i = 0; i < list.length; i++)
Expand All @@ -255,11 +260,6 @@ Buffer.concat = function(list, length) {
length = length >>> 0;
}

if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];

var buffer = new Buffer(length);
var pos = 0;
for (var i = 0; i < list.length; i++) {
Expand Down

0 comments on commit fa105ab

Please sign in to comment.