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

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {TreeNode} from '../util';
import {TreeNode, newArray} from '../util';
export class TreeComponent {
private _renderNodes: any[];
@ -25,7 +25,7 @@ export class TreeComponent {
private _create(parentNode: any, dataNode: TreeNode, index: number) {
if (!this._renderNodes) {
this._renderNodes = new Array(dataNode.transitiveChildCount);
this._renderNodes = newArray(dataNode.transitiveChildCount);
}
const span = document.createElement('span');

View File

@ -68,3 +68,13 @@ export function flattenTree(node: TreeNode, target: TreeNode[] = []): TreeNode[]
}
return target;
}
export function newArray<T = any>(size: number): T[];
export function newArray<T>(size: number, value: T): T[];
export function newArray<T>(size: number, value?: T): T[] {
const list: T[] = [];
for (let i = 0; i < size; i++) {
list.push(value !);
}
return list;
}