From 275450017295761c6ecb6c06da53a73252db5cf1 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Tue, 6 Dec 2022 19:56:06 +0900 Subject: [PATCH] Optimize `Float16Array#toSorted` --- src/Float16Array.mjs | 7 ++++++- src/_util/messages.mjs | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Float16Array.mjs b/src/Float16Array.mjs index 28a26be1..8a652a78 100644 --- a/src/Float16Array.mjs +++ b/src/Float16Array.mjs @@ -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"; @@ -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), @@ -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)); }); diff --git a/src/_util/messages.mjs b/src/_util/messages.mjs index 9786dd5c..c43508ff 100644 --- a/src/_util/messages.mjs +++ b/src/_util/messages.mjs @@ -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";