Skip to content

Commit

Permalink
limit the length of the string to be used for indentation
Browse files Browse the repository at this point in the history
use a cache of precalculated strings instead of generating new ones over and over again
  • Loading branch information
rbri committed Dec 9, 2023
1 parent ef18cb9 commit 52e7ef3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/org/mozilla/javascript/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public abstract class AstNode extends Node implements Comparable<AstNode> {
protected AstNode inlineComment;
private static Map<Integer, String> operatorNames = new HashMap<>();

private static final int MAX_INDENT = 42;
private static final String[] INDENTATIONS = new String[MAX_INDENT + 1];

static {
operatorNames.put(Token.IN, "in");
operatorNames.put(Token.TYPEOF, "typeof");
Expand Down Expand Up @@ -120,6 +123,12 @@ public abstract class AstNode extends Node implements Comparable<AstNode> {
operatorNames.put(Token.ASSIGN_BITXOR, "^=");
operatorNames.put(Token.ASSIGN_EXP, "**=");
operatorNames.put(Token.VOID, "void");

StringBuilder sb = new StringBuilder();
for (int i = 0; i <= MAX_INDENT; i++) {
sb.append(" ");
INDENTATIONS[i] = sb.toString();
}
}

public static class PositionComparator implements Comparator<AstNode>, Serializable {
Expand Down Expand Up @@ -299,11 +308,7 @@ public String toSource() {
* @param indent the number of indentation steps
*/
public String makeIndent(int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append(" ");
}
return sb.toString();
return INDENTATIONS[Math.min(MAX_INDENT, indent)];
}

/** Returns a short, descriptive name for the node, such as "ArrayComprehension". */
Expand Down

0 comments on commit 52e7ef3

Please sign in to comment.