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

@ -10,6 +10,7 @@ import {AotCompilerHost, AotCompilerOptions, GeneratedFile, createAotCompiler, t
import {MetadataBundlerHost} from '@angular/compiler-cli/src/metadata/bundler';
import {MetadataCollector} from '@angular/compiler-cli/src/metadata/collector';
import {ModuleMetadata} from '@angular/compiler-cli/src/metadata/index';
import {newArray} from '@angular/compiler/src/util';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
@ -686,7 +687,7 @@ export function expectNoDiagnostics(program: ts.Program) {
return '';
}
function chars(len: number, ch: string): string { return new Array(len).fill(ch).join(''); }
function chars(len: number, ch: string): string { return newArray(len, ch).join(''); }
function lineNoOf(offset: number, text: string): number {
let result = 1;

View File

@ -9,6 +9,7 @@
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
import * as o from '@angular/compiler/src/output/output_ast';
import {newArray} from '@angular/compiler/src/util';
import {stripSourceMapAndNewLine} from './abstract_emitter_spec';
@ -108,7 +109,7 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some
});
it('should break expressions into multiple lines if they are too long', () => {
const values: o.Expression[] = new Array(100);
const values: o.Expression[] = newArray(100);
values.fill(o.literal(1));
values.splice(50, 0, o.fn([], [new o.ReturnStatement(o.literal(1))]));
expect(emitStmt(o.variable('fn').callFn(values).toStmt())).toEqual([

View File

@ -10,6 +10,7 @@ import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitt
import * as o from '@angular/compiler/src/output/output_ast';
import {JitEmitterVisitor, JitEvaluator} from '@angular/compiler/src/output/output_jit';
import {R3JitReflector} from '@angular/compiler/src/render3/r3_jit';
import {newArray} from '@angular/compiler/src/util';
import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector';
const anotherModuleUrl = 'somePackage/someOtherPath';
@ -18,10 +19,10 @@ const anotherModuleUrl = 'somePackage/someOtherPath';
describe('Output JIT', () => {
describe('regression', () => {
it('should generate unique argument names', () => {
const externalIds = new Array(10).fill(1).map(
const externalIds = newArray(10, 1).map(
(_, index) =>
new o.ExternalReference(anotherModuleUrl, `id_${index}_`, {name: `id_${index}_`}));
const externalIds1 = new Array(10).fill(1).map(
const externalIds1 = newArray(10, 1).map(
(_, index) => new o.ExternalReference(
anotherModuleUrl, `id_${index}_1`, {name: `id_${index}_1`}));
const ctx = EmitterVisitorContext.createRoot();

View File

@ -10,6 +10,8 @@ import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
import * as o from '@angular/compiler/src/output/output_ast';
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler/src/parse_util';
import {newArray} from '@angular/compiler/src/util';
import {stripSourceMapAndNewLine} from './abstract_emitter_spec';
const someGenFilePath = 'somePackage/someGenFile';
@ -160,7 +162,7 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some
});
it('should break expressions into multiple lines if they are too long', () => {
const values: o.Expression[] = new Array(100);
const values: o.Expression[] = newArray(100);
values.fill(o.literal(1));
values.splice(50, 0, o.fn([], [new o.ReturnStatement(o.literal(1))]));
expect(emitStmt(o.variable('fn').callFn(values).toStmt())).toEqual([

View File

@ -19,7 +19,7 @@ import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_refle
import {CompileEntryComponentMetadata, CompileStylesheetMetadata} from '../../src/compile_metadata';
import {Identifiers, createTokenForExternalReference, createTokenForReference} from '../../src/identifiers';
import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../src/ml_parser/interpolation_config';
import {noUndefined} from '../../src/util';
import {newArray, noUndefined} from '../../src/util';
import {MockSchemaRegistry} from '../../testing';
import {unparse} from '../expression_parser/utils/unparser';
import {TEST_COMPILER_PROVIDERS} from '../test_bindings';
@ -481,7 +481,7 @@ class ArrayConsole implements Console {
new BoundDirectivePropertyAst('foo', 'bar', null !, null !)
];
const result = templateVisitAll(visitor, nodes, null);
expect(result).toEqual(new Array(nodes.length).fill(true));
expect(result).toEqual(newArray(nodes.length).fill(true));
});
});