]> zoso.dev Git - buffer.git/commitdiff
Add fast path for Buffer.concat with Uint8Array.
authorKoushik Dutta <koushd@gmail.com>
Wed, 8 Jan 2020 21:27:39 +0000 (13:27 -0800)
committerKoushik Dutta <koushd@gmail.com>
Wed, 8 Jan 2020 21:27:39 +0000 (13:27 -0800)
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

index cb712b17d1edec5cbd690257250690644eac3fdf..4321a91d9ab78869a5a810e63c5c31aa71b58a7d 100644 (file)
--- 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