From 5b7315fa15385efa909224258ec4165bb3e842bd Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 25 Aug 2019 12:39:12 +0200 Subject: [PATCH] [perf] Use `TypedArray#set()` instead of `Buffer#copy()` --- lib/buffer-util.js | 2 +- lib/receiver.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/buffer-util.js b/lib/buffer-util.js index 02973ae39..9de7b6c4b 100644 --- a/lib/buffer-util.js +++ b/lib/buffer-util.js @@ -19,7 +19,7 @@ function concat(list, totalLength) { for (let i = 0; i < list.length; i++) { const buf = list[i]; - buf.copy(target, offset); + target.set(buf, offset); offset += buf.length; } diff --git a/lib/receiver.js b/lib/receiver.js index 28ffca0f5..3a8b92a6b 100644 --- a/lib/receiver.js +++ b/lib/receiver.js @@ -96,11 +96,12 @@ class Receiver extends Writable { do { const buf = this._buffers[0]; + const offset = dst.length - n; if (n >= buf.length) { - this._buffers.shift().copy(dst, dst.length - n); + dst.set(this._buffers.shift(), offset); } else { - buf.copy(dst, dst.length - n, 0, n); + dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset); this._buffers[0] = buf.slice(n); }