Skip to content

Commit

Permalink
bytes: use spread instead of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dchest committed Jun 29, 2024
1 parent 0cc1fd9 commit 8c0ee34
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/bytes/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
/**
* Concatenates byte arrays.
*/
export function concat(...arrays: Uint8Array[]): Uint8Array;
export function concat(): Uint8Array {
export function concat(...arrays: Uint8Array[]): Uint8Array {
// Calculate sum of lengths of all arrays.
let totalLength = 0;
for (let i = 0; i < arguments.length; i++) {
totalLength += arguments[i].length;
for (let i = 0; i < arrays.length; i++) {
totalLength += arrays[i].length;
}

// Allocate new array of calculated length.
const result = new Uint8Array(totalLength);

// Copy all arrays into result.
let offset = 0;
for (let i = 0; i < arguments.length; i++) {
const arg = arguments[i];
for (let i = 0; i < arrays.length; i++) {
const arg = arrays[i];
result.set(arg, offset);
offset += arg.length;
}
Expand Down

0 comments on commit 8c0ee34

Please sign in to comment.