Use bitwise not as Math.floor replacement (#1518)

* Use bitwise not as Math.floor replacement

* No objectSpread in extractTime

* Flatten extractTime calls
This commit is contained in:
Jaco
2022-06-07 08:58:11 +03:00
committed by GitHub
parent 2cd9633afe
commit 7765476a26
7 changed files with 49 additions and 42 deletions
+6
View File
@@ -17,6 +17,12 @@
*/
export function arrayChunk <T> (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<T[]>(outputSize);
for (let i = 0; i < outputSize; i++) {
+5
View File
@@ -19,6 +19,11 @@
* ```
*/
export function arrayFlatten <T> (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;
+5
View File
@@ -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);
});
+3 -1
View File
@@ -5,12 +5,14 @@ export function arrayShuffle <T> (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--;
+2 -1
View File
@@ -24,7 +24,8 @@ export function nSqrt <ExtToBn extends ToBn | ToBigInt> (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
+2 -1
View File
@@ -31,7 +31,8 @@ export function bnSqrt <ExtToBn extends ToBn> (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
+26 -39
View File
@@ -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<Time>, b: Time): Time {
return {
days: a.days + b.days,
hours: a.hours + b.hours,
milliseconds: a.milliseconds + b.milliseconds,
minutes: a.minutes + b.minutes,
seconds: a.seconds + b.seconds
days: (a.days || 0) + b.days,
hours: (a.hours || 0) + b.hours,
milliseconds: (a.milliseconds || 0) + b.milliseconds,
minutes: (a.minutes || 0) + b.minutes,
seconds: (a.seconds || 0) + b.seconds
};
}
function extractDays (milliseconds: number, hrs: number): Time {
const days = Math.floor(hrs / 24);
function extractSecs (ms: number): Time {
const s = ms / 1000;
return addTime(objectSpread({}, ZERO, { days }), extractTime(milliseconds - (days * DAY * 1000)));
}
if (s < 60) {
const seconds = ~~s;
function extractHrs (milliseconds: number, mins: number): Time {
const hrs = mins / 60;
if (hrs < 24) {
const hours = Math.floor(hrs);
return addTime(objectSpread({}, ZERO, { hours }), extractTime(milliseconds - (hours * HRS * 1000)));
return add({ seconds }, extractTime(ms - (seconds * 1000)));
}
return extractDays(milliseconds, hrs);
}
const m = s / 60;
function extractMins (milliseconds: number, secs: number): Time {
const mins = secs / 60;
if (m < 60) {
const minutes = ~~m;
if (mins < 60) {
const minutes = Math.floor(mins);
return addTime(objectSpread({}, ZERO, { minutes }), extractTime(milliseconds - (minutes * 60 * 1000)));
return add({ minutes }, extractTime(ms - (minutes * MIN_MS)));
}
return extractHrs(milliseconds, mins);
}
const h = m / 60;
function extractSecs (milliseconds: number): Time {
const secs = milliseconds / 1000;
if (h < 24) {
const hours = ~~h;
if (secs < 60) {
const seconds = Math.floor(secs);
return addTime(objectSpread({}, ZERO, { seconds }), extractTime(milliseconds - (seconds * 1000)));
return add({ hours }, extractTime(ms - (hours * HR_MS)));
}
return extractMins(milliseconds, secs);
const days = ~~(h / 24);
return add({ days }, extractTime(ms - (days * DAY_MS)));
}
/**
@@ -82,6 +69,6 @@ export function extractTime (milliseconds?: number): Time {
return !milliseconds
? ZERO
: milliseconds < 1000
? objectSpread({}, ZERO, { milliseconds })
? add({ milliseconds }, ZERO)
: extractSecs(milliseconds);
}