Skip to content

Commit

Permalink
Add fast path for Buffer.concat with Uint8Array.
Browse files Browse the repository at this point in the history
Previous implementation would copy the Uint8Array into a new Buffer object,
and then concatenate it. It is faster to use the Uint8Array.set (which Buffer.copy
uses internally) anyways.
  • Loading branch information
koush committed Jan 8, 2020
1 parent 1df9870 commit 1c4e236
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -416,12 +416,16 @@ Buffer.concat = function concat (list, length) {
for (i = 0; i < list.length; ++i) {
var buf = list[i]
if (isInstance(buf, Uint8Array)) {
buf = Buffer.from(buf)
}
if (!Buffer.isBuffer(buf)) {
Uint8Array.prototype.set.call(
buffer,
buf,
pos
)
} else if (!Buffer.isBuffer(buf)) {
throw new TypeError('"list" argument must be an Array of Buffers')
} else {
buf.copy(buffer, pos)
}
buf.copy(buffer, pos)
pos += buf.length
}
return buffer

0 comments on commit 1c4e236

Please sign in to comment.