Skip to content

Commit bf9d748

Browse files
committed
JS: Use StringBuilder when building up type name
This code was a bit of a performance cringe. It copied every character into a temporary array, copied that into a String, and slow-appended that onto another String. Note that the call to Characters.toChars is redundant here as advance() doesn't return a code point; it returns -1 or a UTF-16 char. The -1 case is checked for before reaching the call, so we can just cast it to a char and use it directly. We use a StringBuilder to accumulate the string. Normally it's faster to track the start/end indices and do a substring(), but that won't work in the JSDoc extractor because of the star-skipping logic in advance().
1 parent 179bae8 commit bf9d748

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

javascript/extractor/src/com/semmle/js/parser/JSDocParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ private Token scanNumber() throws ParseError {
561561
private Token scanTypeName() {
562562
char ch, ch2;
563563

564-
value = new String(Character.toChars(advance()));
564+
StringBuilder sb = new StringBuilder();
565+
sb.append((char)advance());
565566
while (index < endIndex && isTypeName(source.charAt(index))) {
566567
ch = source.charAt(index);
567568
if (ch == '.') {
@@ -572,8 +573,9 @@ private Token scanTypeName() {
572573
}
573574
}
574575
}
575-
value += new String(Character.toChars(advance()));
576+
sb.append((char)advance());
576577
}
578+
value = sb.toString();
577579
return Token.NAME;
578580
}
579581

0 commit comments

Comments
 (0)