CKTIS / JavaScript
Typed arrays: `subarray()` view versus `slice()` copy
`subarray()` creates a view of the same buffer, while `slice()` copies the selected elements into a separate buffer by default. Both interpret range bounds the same way.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
typedArray.subarray()
typedArray.subarray(start)
typedArray.subarray(start, end)
typedArray.slice()
typedArray.slice(start)
typedArray.slice(start, end)TypedArray.prototype.subarray/slice refers to two methods on the shared prototype inherited by instances such as Uint16Array. There is no global TypedArray constructor. Both methods require an actual typed array as this; a plain Array causes a TypeError.
Both use zero-based element indices within the view, not byte positions in the buffer as ArrayBuffer.prototype.slice() does. The start is inclusive and the end exclusive. An omitted or undefined start means 0; an omitted or undefined end means the end of the view. Fractions are truncated toward zero, negative indices count back from the end, and bounds are clamped between 0 and the view length. If the normalized end is at or before the start, both methods return an empty typed array for a valid view on a fixed-size buffer when using the default result constructor. A custom constructor[Symbol.species] constructor can return a nonempty typed array even when the requested length is 0.
With the default result constructor, subarray() creates a new view of the same buffer without copying elements. Writes in the overlapping region are visible through either view. Even a small view can keep the entire buffer alive, and its .buffer exposes bytes outside the selected range. Use slice() for an independent copy of the selected elements. A custom constructor[Symbol.species] can run code, change the element type, or change whether the result shares storage. Do not treat these methods as a security boundary for untrusted objects.
In ECMAScript 2024, with the default constructor, calling subarray() on a length-tracking view produces another length-tracking view if end is omitted or undefined. Passing an end other than undefined produces a fixed-length view; slice() produces a fixed-length copy by default. If the buffer is detached or the source view is already out of bounds at call time, slice() throws TypeError. subarray() uses a source length of 0 for its calculations in that case, but constructing the result can still fail. Check resizable-buffer support in your target runtime.
Examples
{
const source = new Uint16Array([10, 20, 30, 40]);
const view = source.subarray(1, -1);
const copy = source.slice(1, -1);
view[0] = 99;
source[2] = 77;
console.log(`source: ${source.join(', ')}`);
console.log(`view: ${view.join(', ')}`);
console.log(`copy: ${copy.join(', ')}`);
console.log(`same buffer: ${view.buffer === source.buffer}, ${copy.buffer === source.buffer}`);
}Result
source: 10, 99, 77, 40 view: 99, 77 copy: 20, 30 same buffer: true, false
`(1, -1)` selects `20` and `30`. Writing through `view` changes `source`, and writing through `source` changes `view`. `copy` keeps `20, 30` and has a separate buffer.
Related lessons
- JavaScript · Lesson 2 · Advanced · Version: 4
Sources
- ECMAScript® 2024 Language Specification — %TypedArray%.prototype.subarray
- ECMAScript® 2024 Language Specification — %TypedArray%.prototype.slice
- ECMAScript® 2024 Language Specification — TypedArraySpeciesCreate
- TypedArray.prototype.subarray() - JavaScript | MDN
- TypedArray.prototype.slice() - JavaScript | MDN
- TypedArray - JavaScript | MDN
- TypedArray[Symbol.species] - JavaScript | MDN
- ArrayBuffer.prototype.slice() - JavaScript | MDN