Skip to content

TypedArray has JavaScript's own methods; subarray shares its elements - #105

Merged
ASDAlexander77 merged 1 commit into
mainfrom
typed-array-methods
Nov 17, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
typed-array-methods

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

TypedArray used to extend Array<T>, so it inherited Array's methods unchanged. That gave several results that differ from JavaScript:

  • sort() compared as text, so [1, 10, 2] stayed 1,10,2.
  • filter, map, slice and the rest returned a plain T[].
  • at didn't take a negative index.
  • There was no subarray, byteLength, byteOffset or BYTES_PER_ELEMENT.

TypedArray is now a class of its own: a window of elements held through a Shared<T[]>. subarray() is a second window onto the same elements, as in JavaScript, and under -mm=own each window holds a count of them.

Changes

  • sort() with no comparator goes by value, and NaN goes last.
  • filter, map, slice, toReversed, toSorted and _with return a TypedArray.
  • fill, reverse, sort and copyWithin work in place and return the array itself.
  • at takes a negative index and returns undefined out of range.
  • includes finds NaN, while indexOf doesn't.
  • new Uint8Array(other) makes a copy.
  • New members: BYTES_PER_ELEMENT (on an instance), byteLength, byteOffset and toLocaleString.
  • entries, keys, values and for...of read through the window.

Behaviour changes

  • The length is fixed, as in JavaScript: push, pop, shift, unshift and splice are gone.
  • An index outside the window reads 0 and a write there does nothing. JavaScript reads undefined, which a numeric element can't hold. Without the check, a subarray could reach the elements around it.

Not included

  • set(array, offset): the indexer needs the name set(index, value), and a class can't overload a method.
  • buffer: there's no ArrayBuffer under a TypedArray.
  • Static from, of and BYTES_PER_ELEMENT: Uint8Array and the others are type aliases, which can't carry static members.

subarray doesn't use the built-in view(), because that copies the slice instead of sharing it.

Testing

  • New test: tests/typed_array_methods.ts.
  • The release suite passes under gc, rc, none and own, compiled and JIT, run locally on Windows.
  • Debug wasn't run locally, because the local debug compiler runtime is out of date. CI will be the first to run it.

🤖 Generated with Claude Code

TypedArray extended Array<T>, so it took Array's methods as they are: sort
compared as text ([1, 10, 2] stayed 1,10,2), filter, map, slice and the
rest gave a plain T[], at took no negative index, and there was no
subarray, byteLength, byteOffset or BYTES_PER_ELEMENT.

It is a class of its own now: a window of elements held through a
Shared<T[]>, so subarray() is a second window onto the same elements, as
in JavaScript, and under -mm=own each window holds a count of them. sort
goes by value with NaN last; filter, map, slice, toReversed, toSorted and
with give a TypedArray; fill, reverse, sort and copyWithin give the array
itself; includes finds NaN; new Uint8Array(other) copies. The length is
fixed, as in JavaScript: push, pop, shift, unshift and splice are gone.
An index outside the window reads 0 and a write there does nothing.

Not here: set(array, offset), whose name the indexer's set(index, value)
takes; buffer; and the static from, of and BYTES_PER_ELEMENT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit badb9c2 into main Nov 17, 2026
5 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant