Skip to content

Commit

Permalink
Optimize: use + operator instead of ToNumber function
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jun 26, 2022
1 parent 7dc9749 commit a98499e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/_util/is.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ArrayIsArray,
MathTrunc,
NativeArrayPrototypeSymbolIterator,
NativeNumber,
NativeSharedArrayBuffer,
NativeTypedArrayPrototypeSymbolIterator,
NumberIsFinite,
Expand Down Expand Up @@ -128,7 +127,7 @@ export function isCanonicalIntegerIndexString(value) {
return false;
}

const number = NativeNumber(value);
const number = +value;
if (value !== number + "") {
return false;
}
Expand Down
2 changes: 0 additions & 2 deletions src/_util/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER =
"Attempting to access detached ArrayBuffer";
export const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT =
"Cannot convert undefined or null to object";
export const CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER =
"Cannot convert a BigInt value to a number";
export const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
"Cannot mix BigInt and other types, use explicit conversions";
export const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
Expand Down
4 changes: 2 additions & 2 deletions src/_util/primordials.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export const {
export const NativeProxy = Proxy;

// Number
export const NativeNumber = Number;
export const {
MAX_SAFE_INTEGER: MAX_SAFE_INTEGER,
isFinite: NumberIsFinite,
isNaN: NumberIsNaN,
} = NativeNumber;
} = Number;

// Symbol
export const {
Expand Down
11 changes: 2 additions & 9 deletions src/_util/spec.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { isObject, isSharedArrayBuffer } from "./is.mjs";
import {
CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER,
THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT,
THIS_IS_NOT_AN_OBJECT,
} from "./messages.mjs";
import {
ArrayBufferPrototypeSlice,
MAX_SAFE_INTEGER,
MathTrunc,
NativeNumber,
NativeTypeError,
NumberIsNaN,
ObjectIs,
SymbolSpecies,
} from "./primordials.mjs";

const MAX_SAFE_INTEGER = NativeNumber.MAX_SAFE_INTEGER;

/**
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
* @param {unknown} target
* @returns {number}
*/
export function ToIntegerOrInfinity(target) {
if (typeof target === "bigint") {
throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER);
}

const number = NativeNumber(target);
const number = +target;

if (NumberIsNaN(number) || number === 0) {
return 0;
Expand Down
23 changes: 7 additions & 16 deletions src/hfround.mjs
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import { convertToNumber, roundToFloat16Bits } from "./_util/converter.mjs";
import { CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER } from "./_util/messages.mjs";
import {
NativeNumber,
NativeTypeError,
NumberIsFinite,
} from "./_util/primordials.mjs";
import { NumberIsFinite } from "./_util/primordials.mjs";

/**
* returns the nearest half-precision float representation of a number
*
* @param {number} num
* @param {number} x
* @returns {number}
*/
export function hfround(num) {
if (typeof num === "bigint") {
throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER);
}

num = NativeNumber(num);
export function hfround(x) {
const number = +x;

// for optimization
if (!NumberIsFinite(num) || num === 0) {
return num;
if (!NumberIsFinite(number) || number === 0) {
return number;
}

const x16 = roundToFloat16Bits(num);
const x16 = roundToFloat16Bits(number);
return convertToNumber(x16);
}

0 comments on commit a98499e

Please sign in to comment.