perf: don't create holey arrays (#32155)

Don't use `Array` constructor with the size value (ex. `new Array(5)`) - this will create a `HOLEY_ELEMENTS` array (even if this array is filled in later on!);

https://v8.dev/blog/elements-kinds
https://stackoverflow.com/questions/32054170/how-to-resize-an-array

PR Close #32155
This commit is contained in:
Miško Hevery
2019-08-15 13:42:17 -07:00
committed by Andrew Kushnir
parent c957dfc167
commit 64770571b2
32 changed files with 137 additions and 83 deletions

View File

@ -29,7 +29,7 @@ export function sha1Binary(buffer: ArrayBuffer): string {
}
function _sha1(words32: number[], len: number): string {
const w = new Array(80);
const w: number[] = [];
let [a, b, c, d, e]: number[] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
words32[len >> 5] |= 0x80 << (24 - len % 32);
@ -113,9 +113,10 @@ function fk(index: number, b: number, c: number, d: number): [number, number] {
function stringToWords32(str: string, endian: Endian): number[] {
const words32 = Array((str.length + 3) >>> 2);
const size = (str.length + 3) >>> 2;
const words32 = [];
for (let i = 0; i < words32.length; i++) {
for (let i = 0; i < size; i++) {
words32[i] = wordAt(str, i * 4, endian);
}
@ -123,9 +124,10 @@ function stringToWords32(str: string, endian: Endian): number[] {
}
function arrayBufferToWords32(buffer: ArrayBuffer, endian: Endian): number[] {
const words32 = Array((buffer.byteLength + 3) >>> 2);
const size = (buffer.byteLength + 3) >>> 2;
const words32: number[] = [];
const view = new Uint8Array(buffer);
for (let i = 0; i < words32.length; i++) {
for (let i = 0; i < size; i++) {
words32[i] = wordAt(view, i * 4, endian);
}
return words32;

View File

@ -29,7 +29,7 @@ export function sha1Binary(buffer: ArrayBuffer): string {
}
function _sha1(words32: number[], len: number): string {
const w = new Array(80);
const w: number[] = [];
let [a, b, c, d, e]: number[] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
words32[len >> 5] |= 0x80 << (24 - len % 32);
@ -113,9 +113,10 @@ function fk(index: number, b: number, c: number, d: number): [number, number] {
function stringToWords32(str: string, endian: Endian): number[] {
const words32 = Array((str.length + 3) >>> 2);
const size = (str.length + 3) >>> 2;
const words32 = [];
for (let i = 0; i < words32.length; i++) {
for (let i = 0; i < size; i++) {
words32[i] = wordAt(str, i * 4, endian);
}
@ -123,9 +124,10 @@ function stringToWords32(str: string, endian: Endian): number[] {
}
function arrayBufferToWords32(buffer: ArrayBuffer, endian: Endian): number[] {
const words32 = Array((buffer.byteLength + 3) >>> 2);
const size = (buffer.byteLength + 3) >>> 2;
const words32: number[] = [];
const view = new Uint8Array(buffer);
for (let i = 0; i < words32.length; i++) {
for (let i = 0; i < size; i++) {
words32[i] = wordAt(view, i * 4, endian);
}
return words32;