webtorrent/webtorrent

perf: use uint8.subarray instead of uint8.slice

ThaUnknown opened this issue · 1 comments

when migrating to uint8 from node buffer, code which used .slice was not changed, node:buffer's slice creates a subarray, but uint8.slice creates a copy, this needs to be updated to use .subarray, this would be another very big performance gain

// Original code with Node.js Buffer
const originalBuffer = Buffer.from(/* some data */);
const subBuffer = originalBuffer.slice(10, 20); // Creates a subarray

// Updated code using Uint8Array
const uint8Array = new Uint8Array(/* some data */);
const subArray = uint8Array.subarray(10, 20); // Creates a view without copying