Skip to content

Commit

Permalink
Optimize Float16Array#toSorted
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Dec 6, 2022
1 parent 61154f7 commit 2754500
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
OFFSET_IS_OUT_OF_BOUNDS,
REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE,
SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT,
THE_COMPARISON_FUNCTION_MUST_BE_EITHER_A_FUNCTION_OR_UNDEFINED,
THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY,
THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT,
} from "./_util/messages.mjs";
Expand Down Expand Up @@ -937,6 +938,11 @@ export class Float16Array {
assertFloat16Array(this);
const float16bitsArray = getFloat16BitsArray(this);

if (compareFn !== undefined && typeof compareFn !== "function") {
throw new NativeTypeError(THE_COMPARISON_FUNCTION_MUST_BE_EITHER_A_FUNCTION_OR_UNDEFINED);
}
const sortCompare = compareFn !== undefined ? compareFn : defaultCompare;

// don't use SpeciesConstructor
const uint16 = new NativeUint16Array(
TypedArrayPrototypeGetBuffer(float16bitsArray),
Expand All @@ -950,7 +956,6 @@ export class Float16Array {
);

const clonedFloat16bitsArray = getFloat16BitsArray(cloned);
const sortCompare = compareFn !== undefined ? compareFn : defaultCompare;
TypedArrayPrototypeSort(clonedFloat16bitsArray, (x, y) => {
return sortCompare(convertToNumber(x), convertToNumber(y));
});
Expand Down
2 changes: 2 additions & 0 deletions src/_util/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
export const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
export const REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE =
"Reduce of empty array with no initial value";
export const THE_COMPARISON_FUNCTION_MUST_BE_EITHER_A_FUNCTION_OR_UNDEFINED =
"The comparison function must be either a function or undefined";
export const OFFSET_IS_OUT_OF_BOUNDS = "Offset is out of bounds";

0 comments on commit 2754500

Please sign in to comment.