From 1c4e236b3892ede9977f552dd8d76ddd9798f314 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Wed, 8 Jan 2020 13:27:39 -0800 Subject: [PATCH] Add fast path for Buffer.concat with Uint8Array. 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. --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cb712b1..4321a91 100644 --- a/index.js +++ b/index.js @@ -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 -- 2.34.1