Skip to content

Commit

Permalink
perf(codec): avoid re-construct when concat bytes (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Jul 28, 2022
1 parent d91a96f commit 1669bf5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/codec/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ export function hexify(buf: BytesLike): string {
}

export function concat(...bytesLikes: BytesLike[]): Uint8Array {
return Uint8Array.from(
bytesLikes.flatMap((bytes) => Array.from(bytify(bytes)))
);
const unmerged = bytesLikes.map(bytify);
const totalSize = unmerged.reduce((size, item) => size + item.length, 0);

const merged = new Uint8Array(totalSize);

let offset = 0;
unmerged.forEach((item) => {
merged.set(item, offset);
offset += item.length;
});

return merged;
}

// export function split(bytes: BytesLike, points: number[]): Uint8Array[] {
Expand Down

1 comment on commit 1669bf5

@vercel
Copy link

@vercel vercel bot commented on 1669bf5 Jul 28, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

lumos-website – ./

lumos-website-cryptape.vercel.app
lumos-website-git-develop-cryptape.vercel.app
lumos-website.vercel.app

Please sign in to comment.