Small performance adjustments for Uint8Array indexing (#1603)
This commit is contained in:
@@ -43,5 +43,5 @@ describe('isAscii', (): void => {
|
||||
});
|
||||
|
||||
perf('isAscii (str)', 2_000_000, [['Hello World! Testing']], isAscii);
|
||||
perf('isAscii (u8a)', 100_000, [[[stringToU8a('Hello World! Testing')]]], isAscii);
|
||||
perf('isAscii (u8a)', 200_000, [[[stringToU8a('Hello World! Testing')]]], isAscii);
|
||||
});
|
||||
|
||||
@@ -9,13 +9,13 @@ import { isString } from './string';
|
||||
|
||||
/** @internal */
|
||||
function isAsciiStr (str: AnyString): boolean {
|
||||
const count = str.length;
|
||||
const count = str.length | 0;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const b = str.charCodeAt(i);
|
||||
|
||||
// check is inlined here, it is faster than making a call
|
||||
if ((b < 32) || (b > 126)) {
|
||||
if (b < 32 || b > 126) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -25,13 +25,13 @@ function isAsciiStr (str: AnyString): boolean {
|
||||
|
||||
/** @internal */
|
||||
function isAsciiBytes (u8a: Uint8Array | Buffer | number[]): boolean {
|
||||
const count = u8a.length;
|
||||
const count = u8a.length | 0;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const b = u8a[i];
|
||||
const b = u8a[i] | 0;
|
||||
|
||||
// check is inlined here, it is faster than making a call
|
||||
if ((b < 32) || (b > 126)) {
|
||||
if (b < 32 || b > 126) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { perf } from '../test/performance';
|
||||
import { u8aCmp } from '.';
|
||||
|
||||
const ltest = new Uint8Array(32768);
|
||||
const stest = new Uint8Array(256);
|
||||
const ztest = new Uint8Array(64);
|
||||
|
||||
for (let i = 0; i < ltest.length; i++) {
|
||||
if (i < ztest.length) {
|
||||
ztest[i] = i % 256;
|
||||
}
|
||||
|
||||
if (i < stest.length) {
|
||||
stest[i] = i % 256;
|
||||
}
|
||||
|
||||
ltest[i] = i % 256;
|
||||
}
|
||||
|
||||
describe('u8aCmp', (): void => {
|
||||
it('has the same result as localCompare (equal length)', (): void => {
|
||||
const a = 'test abc';
|
||||
@@ -36,4 +53,8 @@ describe('u8aCmp', (): void => {
|
||||
)
|
||||
).toEqual(0);
|
||||
});
|
||||
|
||||
perf('u8aCmp (64 cmp)', 100_000, [[ztest, ztest]], u8aCmp);
|
||||
perf('u8aCmp (256 cmp)', 50_000, [[stest, stest]], u8aCmp);
|
||||
perf('u8aCmp (32k cmp)', 500, [[ltest, ltest]], u8aCmp);
|
||||
});
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
|
||||
*/
|
||||
export function u8aEmpty (value: Uint8Array): boolean {
|
||||
const len = value.length;
|
||||
const len = value.length | 0;
|
||||
|
||||
// on smaller sizes, the byte-by-byte compare is faster than allocating
|
||||
// another object for DataView (on very large arrays the DataView is faster)
|
||||
for (let i = 0; i < len; i++) {
|
||||
if (value[i]) {
|
||||
if (value[i] | 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8A
|
||||
if (u8aa.length === u8ab.length) {
|
||||
const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
|
||||
const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
|
||||
const mod = u8aa.length % 4;
|
||||
const length = u8aa.length - mod;
|
||||
const mod = (u8aa.length % 4) | 0;
|
||||
const length = (u8aa.length - mod) | 0;
|
||||
|
||||
for (let i = 0; i < length; i += 4) {
|
||||
if (dvA.getUint32(i) !== dvB.getUint32(i)) {
|
||||
|
||||
Reference in New Issue
Block a user