Skip to content

Commit

Permalink
[perf] Use TypedArray#set() instead of Buffer#copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Aug 25, 2019
1 parent 74bac8e commit 5b7315f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/buffer-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

2 comments on commit 5b7315f

@murataka
Copy link

@murataka murataka commented on 5b7315f Jun 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpinca , I possibly did not get the point , but there's this benchmark test :

https://gist.github.com/andreasgal/f570229911ba52c35fde8d07c34e20b5

Maybe some more minor optimizations can be done ?

@lpinca
Copy link
Member Author

@lpinca lpinca commented on 5b7315f Jun 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.