Skip to content

Commit

Permalink
[bun] Improve performance of TypedArray.from
Browse files Browse the repository at this point in the history
This uses `TypedArray.prototype.set` when `TypedArray.from(array)` receives another TypedArray

It does not handle when `set` has been overridden. This will likely have to be changed later, but it is better than the present state
  • Loading branch information
Jarred-Sumner committed May 1, 2022
1 parent b06577c commit def720f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Source/JavaScriptCore/builtins/TypedArrayConstructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ function from(items /* [ , mapfn [ , thisArg ] ] */)
var arrayLike = @toObject(items, "TypedArray.from requires an array-like object - not null or undefined");

var iteratorMethod = items.@@iterator;

if (!mapFn && @isTypedArrayView(arrayLike)) {
var arrayLikeLength = @toLength(arrayLike.length);

var result = new this(arrayLikeLength);
if (@typedArrayLength(result) < arrayLikeLength)
@throwTypeError("TypedArray.from constructed typed array of insufficient length");

// This is not precise enough I think?
// .slice() isn't exactly right because the arrays could be a different type
this.prototype.set.@call(result, arrayLike);
return result;
}

if (!@isUndefinedOrNull(iteratorMethod)) {
if (!@isCallable(iteratorMethod))
@throwTypeError("TypedArray.from requires that the property of the first argument, items[Symbol.iterator], when exists, be a function");
Expand Down

0 comments on commit def720f

Please sign in to comment.