diff --git a/packages/util/src/array/filter.ts b/packages/util/src/array/filter.ts index 3d7e0d1a6..304ec9ea4 100644 --- a/packages/util/src/array/filter.ts +++ b/packages/util/src/array/filter.ts @@ -21,8 +21,8 @@ import { isUndefined } from '../is/undefined'; */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function arrayFilter (array: T[], allowNulls = true): T[] { - return array.filter((value) => - !isUndefined(value) && - (allowNulls || !isNull(value)) + return array.filter((v) => + !isUndefined(v) && + (allowNulls || !isNull(v)) ); } diff --git a/packages/util/src/bi/toHex.ts b/packages/util/src/bi/toHex.ts index 9358f3fef..7be93e52f 100644 --- a/packages/util/src/bi/toHex.ts +++ b/packages/util/src/bi/toHex.ts @@ -19,15 +19,13 @@ const ZERO_STR = '0x00'; * @summary Creates a hex value from a bigint object. */ export function nToHex (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString { - if (!value) { - return ZERO_STR; - } - - return u8aToHex( - nToU8a(value, objectSpread( - // We spread here, the default for hex values is BE (JSONRPC via substrate) - { isLe: false, isNegative: false }, - options - )) - ); + return !value + ? ZERO_STR + : u8aToHex( + nToU8a(value, objectSpread( + // We spread here, the default for hex values is BE (JSONRPC via substrate) + { isLe: false, isNegative: false }, + options + )) + ); } diff --git a/packages/util/src/bn/toHex.ts b/packages/util/src/bn/toHex.ts index 8fd451da2..8f09191cb 100644 --- a/packages/util/src/bn/toHex.ts +++ b/packages/util/src/bn/toHex.ts @@ -34,19 +34,17 @@ const DEFAULT_OPTS: Options = { bitLength: -1, isLe: false, isNegative: false }; function bnToHex (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString; function bnToHex (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString; function bnToHex (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = DEFAULT_OPTS, arg2?: boolean): HexString { - if (!value) { - return ZERO_STR; - } - - return u8aToHex( - bnToU8a(value, objectSpread( - // We spread here, the default for hex values is BE (JSONRPC via substrate) - { isLe: false, isNegative: false }, - isNumber(arg1) - ? { bitLength: arg1, isLe: arg2 } - : arg1 - )) - ); + return !value + ? ZERO_STR + : u8aToHex( + bnToU8a(value, objectSpread( + // We spread here, the default for hex values is BE (JSONRPC via substrate) + { isLe: false, isNegative: false }, + isNumber(arg1) + ? { bitLength: arg1, isLe: arg2 } + : arg1 + )) + ); } export { bnToHex }; diff --git a/packages/util/src/extractTime.ts b/packages/util/src/extractTime.ts index f07d7c70a..8c257645e 100644 --- a/packages/util/src/extractTime.ts +++ b/packages/util/src/extractTime.ts @@ -7,6 +7,7 @@ import { objectSpread } from './object/spread'; const HRS = 60 * 60; const DAY = HRS * 24; +const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 }; /** * @name addTime @@ -23,8 +24,6 @@ function addTime (a: Time, b: Time): Time { }; } -const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 }; - function extractDays (milliseconds: number, hrs: number): Time { const days = Math.floor(hrs / 24); @@ -80,11 +79,9 @@ function extractSecs (milliseconds: number): Time { * ``` */ export function extractTime (milliseconds?: number): Time { - if (!milliseconds) { - return ZERO; - } else if (milliseconds < 1000) { - return objectSpread({}, ZERO, { milliseconds }); - } - - return extractSecs(milliseconds); + return !milliseconds + ? ZERO + : milliseconds < 1000 + ? objectSpread({}, ZERO, { milliseconds }) + : extractSecs(milliseconds); } diff --git a/packages/util/src/hex/addPrefix.ts b/packages/util/src/hex/addPrefix.ts index 603dfaea1..3c829e330 100644 --- a/packages/util/src/hex/addPrefix.ts +++ b/packages/util/src/hex/addPrefix.ts @@ -20,9 +20,7 @@ import { hexHasPrefix } from './hasPrefix'; * ``` */ export function hexAddPrefix (value?: string | null): HexString { - if (value && hexHasPrefix(value)) { - return value; - } - - return `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`; + return value && hexHasPrefix(value) + ? value + : `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`; } diff --git a/packages/util/src/hex/toBigInt.ts b/packages/util/src/hex/toBigInt.ts index b99138949..e58e3396e 100644 --- a/packages/util/src/hex/toBigInt.ts +++ b/packages/util/src/hex/toBigInt.ts @@ -14,12 +14,10 @@ import { hexToU8a } from './toU8a'; * @summary Creates a BigInt instance object from a hex string. */ export function hexToBigInt (value?: string | null, options: ToBnOptions = {}): bigint { - if (!value || value === '0x') { - return BigInt(0); - } - - return u8aToBigInt( - hexToU8a(value), - objectSpread({ isLe: false, isNegative: false }, options) - ); + return !value || value === '0x' + ? BigInt(0) + : u8aToBigInt( + hexToU8a(value), + objectSpread({ isLe: false, isNegative: false }, options) + ); } diff --git a/packages/util/src/number/toU8a.ts b/packages/util/src/number/toU8a.ts index 0560afdc5..60cc2efe0 100644 --- a/packages/util/src/number/toU8a.ts +++ b/packages/util/src/number/toU8a.ts @@ -21,11 +21,9 @@ import { numberToHex } from './toHex'; * ``` */ export function numberToU8a (value?: number | null, bitLength = -1): Uint8Array { - if (isUndefined(value) || isNull(value) || isNaN(value)) { - return new Uint8Array(); - } - - return hexToU8a( - numberToHex(value, bitLength) - ); + return isUndefined(value) || isNull(value) || isNaN(value) + ? new Uint8Array() + : hexToU8a( + numberToHex(value, bitLength) + ); } diff --git a/packages/util/src/u8a/empty.spec.ts b/packages/util/src/u8a/empty.spec.ts index ac13a1842..2c04093a8 100644 --- a/packages/util/src/u8a/empty.spec.ts +++ b/packages/util/src/u8a/empty.spec.ts @@ -1,8 +1,11 @@ // Copyright 2017-2022 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { performance } from '../test/performance'; import { u8aEmpty } from '.'; +const ztest = new Uint8Array(32); + describe('u8aEmpty', (): void => { it('returns true on zero length', (): void => { expect( @@ -21,4 +24,6 @@ describe('u8aEmpty', (): void => { u8aEmpty(new Uint8Array([0, 0, 0, 0, 0, 1])) ).toEqual(false); }); + + performance('u8aEmpty (32 cmp)', 1000000, [[ztest]], u8aEmpty); }); diff --git a/packages/util/src/u8a/empty.ts b/packages/util/src/u8a/empty.ts index 1915517b2..d8b9d1c37 100644 --- a/packages/util/src/u8a/empty.ts +++ b/packages/util/src/u8a/empty.ts @@ -8,6 +8,9 @@ * Checks to see if the input `Uint8Array` has zero length or contains all 0 values. */ export function u8aEmpty (value: Uint8Array): boolean { + // on smaller values < 64 bytes, the byte-by-byte compare is faster than + // allocating yet another object for DataView (on large buffers the DataView + // is much faster) for (let i = 0; i < value.length; i++) { if (value[i]) { return false;