Repository navigation
TypedArray has JavaScript's own methods; subarray shares its elements - #105
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]stayed1,10,2.filter,map,sliceand the rest returned a plainT[].atdidn't take a negative index.subarray,byteLength,byteOffsetorBYTES_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=owneach window holds a count of them.Changes
sort()with no comparator goes by value, and NaN goes last.filter,map,slice,toReversed,toSortedand_withreturn a TypedArray.fill,reverse,sortandcopyWithinwork in place and return the array itself.attakes a negative index and returnsundefinedout of range.includesfinds NaN, whileindexOfdoesn't.new Uint8Array(other)makes a copy.BYTES_PER_ELEMENT(on an instance),byteLength,byteOffsetandtoLocaleString.entries,keys,valuesandfor...ofread through the window.Behaviour changes
push,pop,shift,unshiftandspliceare gone.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 nameset(index, value), and a class can't overload a method.buffer: there's no ArrayBuffer under a TypedArray.from,ofandBYTES_PER_ELEMENT:Uint8Arrayand the others are type aliases, which can't carry static members.subarraydoesn't use the built-inview(), because that copies the slice instead of sharing it.Testing
tests/typed_array_methods.ts.🤖 Generated with Claude Code