From 7765476a26c8e5043a5f95eae2dc434db533284a Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 7 Jun 2022 07:58:11 +0200 Subject: [PATCH] Use bitwise not as Math.floor replacement (#1518) * Use bitwise not as Math.floor replacement * No objectSpread in extractTime * Flatten extractTime calls --- packages/util/src/array/chunk.ts | 6 +++ packages/util/src/array/flatten.ts | 5 ++ packages/util/src/array/shuffle.spec.ts | 5 ++ packages/util/src/array/shuffle.ts | 4 +- packages/util/src/bi/sqrt.ts | 3 +- packages/util/src/bn/sqrt.ts | 3 +- packages/util/src/extractTime.ts | 65 ++++++++++--------------- 7 files changed, 49 insertions(+), 42 deletions(-) diff --git a/packages/util/src/array/chunk.ts b/packages/util/src/array/chunk.ts index 246208321..df6600d65 100644 --- a/packages/util/src/array/chunk.ts +++ b/packages/util/src/array/chunk.ts @@ -17,6 +17,12 @@ */ export function arrayChunk (array: T[], chunkSize: number): T[][] { const outputSize = Math.ceil(array.length / chunkSize); + + // noop for the single-split case + if (outputSize === 1) { + return [array]; + } + const output = Array(outputSize); for (let i = 0; i < outputSize; i++) { diff --git a/packages/util/src/array/flatten.ts b/packages/util/src/array/flatten.ts index 4abe55c99..3d6321b1b 100644 --- a/packages/util/src/array/flatten.ts +++ b/packages/util/src/array/flatten.ts @@ -19,6 +19,11 @@ * ``` */ export function arrayFlatten (arrays: T[][]): T[] { + // noop for the single-entry case + if (arrays.length === 1) { + return arrays[0]; + } + // pre-allocate based on the combined size let size = 0; diff --git a/packages/util/src/array/shuffle.spec.ts b/packages/util/src/array/shuffle.spec.ts index db63d61c9..d3c83c774 100644 --- a/packages/util/src/array/shuffle.spec.ts +++ b/packages/util/src/array/shuffle.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 { arrayRange, arrayShuffle } from '.'; +const ptest = arrayRange(16284); + describe('arrayShuffle', (): void => { it('returns an empty array as-is', (): void => { expect( @@ -28,4 +31,6 @@ describe('arrayShuffle', (): void => { JSON.stringify(inp) ).not.toEqual(JSON.stringify(out)); }); + + performance('arrayShuffle', 1000, [[ptest]], arrayShuffle); }); diff --git a/packages/util/src/array/shuffle.ts b/packages/util/src/array/shuffle.ts index e45baa964..f6e2e5c44 100644 --- a/packages/util/src/array/shuffle.ts +++ b/packages/util/src/array/shuffle.ts @@ -5,12 +5,14 @@ export function arrayShuffle (input: T[]): T[] { const result = input.slice(); let curr = result.length; + // noop for the single entry if (curr === 1) { return result; } while (curr !== 0) { - const rand = Math.floor(Math.random() * curr); + // ~~ is more performant than Math.floor + const rand = ~~(Math.random() * curr); curr--; diff --git a/packages/util/src/bi/sqrt.ts b/packages/util/src/bi/sqrt.ts index 8252705fb..456739bd3 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -24,7 +24,8 @@ export function nSqrt (value: ExtToBn | BN | b // https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/ // shortcut <= 2^53 - 1 to use the JS utils if (n <= _2pow53n) { - return BigInt(Math.floor(Math.sqrt(Number(n)))); + // ~~ is more performant that Math.floor + return BigInt(~~Math.sqrt(Number(n))); } // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the diff --git a/packages/util/src/bn/sqrt.ts b/packages/util/src/bn/sqrt.ts index 9aa299de6..b55080a81 100644 --- a/packages/util/src/bn/sqrt.ts +++ b/packages/util/src/bn/sqrt.ts @@ -31,7 +31,8 @@ export function bnSqrt (value: ExtToBn | BN | bigint | st // https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/ // shortcut <= 2^53 - 1 to use the JS utils if (n.lte(BN_MAX_INTEGER)) { - return new BN(Math.floor(Math.sqrt(n.toNumber()))); + // ~~ More performant version of Math.floor + return new BN(~~Math.sqrt(n.toNumber())); } // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the diff --git a/packages/util/src/extractTime.ts b/packages/util/src/extractTime.ts index 8c257645e..d488b01b1 100644 --- a/packages/util/src/extractTime.ts +++ b/packages/util/src/extractTime.ts @@ -3,10 +3,9 @@ import type { Time } from './types'; -import { objectSpread } from './object/spread'; - -const HRS = 60 * 60; -const DAY = HRS * 24; +const MIN_MS = 60 * 1000; +const HR_MS = MIN_MS * 60; +const DAY_MS = HR_MS * 24; const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 }; /** @@ -14,56 +13,44 @@ const ZERO: Time = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 * @summary Add together two Time arrays */ -function addTime (a: Time, b: Time): Time { +function add (a: Partial