From d170cdfbed497986c6fcf0c12d9def915c629052 Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Fri, 17 Nov 2023 10:56:18 -0700 Subject: [PATCH] Initialize ArrayList with expected size whenever possible. --- .../internal/TypeScriptParserVisitor.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java index e65a5aa4..1b0423a6 100644 --- a/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java +++ b/src/main/java/org/openrewrite/javascript/internal/TypeScriptParserVisitor.java @@ -67,9 +67,10 @@ public TypeScriptParserVisitor(TSCNode source, TSCSourceFileContext sourceContex } public JS.CompilationUnit visitSourceFile() { - List> statements = new ArrayList<>(); + TSCNodeList statementList = source.getNodeListProperty("statements"); + List> statements = new ArrayList<>(statementList.size()); Space prefix = whitespace(); - for (TSCNode child : source.getNodeListProperty("statements")) { + for (TSCNode child : statementList) { @Nullable J visited; int saveCursor = getCursor(); try { @@ -243,7 +244,7 @@ private J.NewArray visitArrayLiteralExpression(TSCNode node) { this::visitNode, true ); - List> elements = new ArrayList<>(); + List> elements = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement(); @@ -554,7 +555,7 @@ private J.MethodInvocation visitCallExpression(TSCNode node) { true ); - List> typeParams = new ArrayList<>(); + List> typeParams = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement(); @@ -574,7 +575,7 @@ private J.MethodInvocation visitCallExpression(TSCNode node) { this::visitNode, true ); - List> elements = new ArrayList<>(); + List> elements = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement(); @@ -842,7 +843,7 @@ private J.Annotation visitDecorator(TSCNode node) { true ); - List> elements = new ArrayList<>(); + List> elements = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement(); @@ -1644,7 +1645,7 @@ private J.NewClass visitNewExpression(TSCNode node) { this::visitNode, true ); - List> elements = new ArrayList<>(); + List> elements = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement(); @@ -1695,7 +1696,7 @@ private J visitObjectLiteralExpression(TSCNode node) { if (jContainer.getElements().isEmpty()) { arguments = JContainer.empty(); } else { - List> elements = new ArrayList<>(); + List> elements = new ArrayList<>(jContainer.getElements().size()); for (JRightPadded jjRightPadded : jContainer.getPadding().getElements()) { Expression exp = (!(jjRightPadded.getElement() instanceof Expression) && jjRightPadded.getElement() instanceof Statement) ? new JS.StatementExpression(randomId(), (Statement) jjRightPadded.getElement()) : (Expression) jjRightPadded.getElement();