Skip to content

Commit 4f037c1

Browse files
vvnsrznVivianHublo
andauthored
⚡️ uint8array set method instead of spread (#1667)
Hello @coyotte508 I stumbled upon this [issue](#1280). I suggest to use the [set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set) method instead. Benchmark: https://jsben.ch/jze3P --------- Co-authored-by: VivianHublo <[email protected]>
1 parent 0eceb78 commit 4f037c1

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

packages/hub/src/utils/XetBlob.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApiError } from "../error";
22
import type { CredentialsParams } from "../types/public";
33
import { checkCredentials } from "./checkCredentials";
4+
import { combineUint8Arrays } from "./combineUint8Arrays";
45
import { decompress as lz4_decompress } from "../vendor/lz4js";
56
import { RangeList } from "./RangeList";
67

@@ -327,11 +328,11 @@ export class XetBlob extends Blob {
327328
totalFetchBytes += result.value.byteLength;
328329

329330
if (leftoverBytes) {
330-
result.value = new Uint8Array([...leftoverBytes, ...result.value]);
331+
result.value = combineUint8Arrays(leftoverBytes, result.value);
331332
leftoverBytes = undefined;
332333
}
333334

334-
while (totalBytesRead < maxBytes && result.value.byteLength) {
335+
while (totalBytesRead < maxBytes && result.value?.byteLength) {
335336
if (result.value.byteLength < 8) {
336337
// We need 8 bytes to parse the chunk header
337338
leftoverBytes = result.value;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, it, expect } from "vitest";
2+
import { combineUint8Arrays } from "./combineUint8Arrays";
3+
4+
describe("combineUint8Arrays", () => {
5+
it.each([
6+
{ a: [], b: [], expected: [] },
7+
{ a: [], b: [1, 2, 3], expected: [1, 2, 3] },
8+
{ a: [4, 5, 6], b: [], expected: [4, 5, 6] },
9+
{ a: [7, 8], b: [9, 10], expected: [7, 8, 9, 10] },
10+
{ a: [1], b: [2, 3, 4], expected: [1, 2, 3, 4] },
11+
])("combines $a and $b to $expected", ({ a, b, expected }) => {
12+
const result = combineUint8Arrays(new Uint8Array(a), new Uint8Array(b));
13+
expect(result).toEqual(new Uint8Array(expected));
14+
});
15+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function combineUint8Arrays(
2+
a: Uint8Array<ArrayBufferLike>,
3+
b: Uint8Array<ArrayBufferLike>
4+
): Uint8Array<ArrayBuffer> {
5+
const aLength = a.length;
6+
const combinedBytes = new Uint8Array(aLength + b.length);
7+
combinedBytes.set(a);
8+
combinedBytes.set(b, aLength);
9+
return combinedBytes;
10+
}

0 commit comments

Comments
 (0)