Files
shaka-player/test/util/uint8array_utils_unit.js
T
Ivan 2c8cfd20f3 perf(TS): Eliminate per-PES wrapper array allocations in parse loop (#9883)
This PR reduces GC pressure during MPEG-TS segment parsing - it replaces
nested PES packet arrays with a flat array plus a PES-start index,
eliminating per-PES inner array allocations and avoiding spread-operator
overhead when concatenating packets - reduces allocations, hence reduces
GC pressure on low-end devices during long livestreams
2026-03-26 10:03:32 +01:00

94 lines
2.8 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('Uint8ArrayUtils', () => {
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
describe('concat', () => {
it('concatenates two buffers', () => {
const a = new Uint8Array([1, 2]);
const b = new Uint8Array([3, 4]);
expect(Uint8ArrayUtils.concat(a, b))
.toEqual(new Uint8Array([1, 2, 3, 4]));
});
it('concatenates three buffers', () => {
const a = new Uint8Array([1]);
const b = new Uint8Array([2]);
const c = new Uint8Array([3]);
expect(Uint8ArrayUtils.concat(a, b, c))
.toEqual(new Uint8Array([1, 2, 3]));
});
it('handles empty buffers', () => {
const a = new Uint8Array([1, 2]);
const b = new Uint8Array([]);
const c = new Uint8Array([3]);
expect(Uint8ArrayUtils.concat(a, b, c))
.toEqual(new Uint8Array([1, 2, 3]));
});
it('returns empty array when called with no args', () => {
expect(Uint8ArrayUtils.concat()).toEqual(new Uint8Array([]));
});
it('accepts ArrayBuffer inputs', () => {
const a = shaka.util.BufferUtils.toArrayBuffer(new Uint8Array([1, 2]));
const b = shaka.util.BufferUtils.toArrayBuffer(new Uint8Array([3, 4]));
expect(Uint8ArrayUtils.concat(a, b))
.toEqual(new Uint8Array([1, 2, 3, 4]));
});
});
describe('concatRange', () => {
it('concatenates the full array by default', () => {
const arr = [new Uint8Array([1, 2]), new Uint8Array([3, 4])];
expect(Uint8ArrayUtils.concatRange(arr))
.toEqual(new Uint8Array([1, 2, 3, 4]));
});
it('concatenates a middle range', () => {
const arr = [
new Uint8Array([1, 2]),
new Uint8Array([3, 4]),
new Uint8Array([5, 6]),
];
expect(Uint8ArrayUtils.concatRange(arr, 1, 2))
.toEqual(new Uint8Array([3, 4]));
});
it('concatenates from start with explicit end', () => {
const arr = [
new Uint8Array([1]),
new Uint8Array([2]),
new Uint8Array([3]),
];
expect(Uint8ArrayUtils.concatRange(arr, 0, 2))
.toEqual(new Uint8Array([1, 2]));
});
it('concatenates from explicit start to end of array', () => {
const arr = [
new Uint8Array([1]),
new Uint8Array([2]),
new Uint8Array([3]),
];
expect(Uint8ArrayUtils.concatRange(arr, 1))
.toEqual(new Uint8Array([2, 3]));
});
it('returns empty array for empty range', () => {
const arr = [new Uint8Array([1, 2]), new Uint8Array([3, 4])];
expect(Uint8ArrayUtils.concatRange(arr, 1, 1))
.toEqual(new Uint8Array([]));
});
it('returns empty array for empty input', () => {
expect(Uint8ArrayUtils.concatRange([])).toEqual(new Uint8Array([]));
});
});
});