From bf9d7484e41dd8b8078f31564710ee83ebbdd722 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 20 Mar 2025 09:43:10 +0100 Subject: [PATCH] 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(). --- .../extractor/src/com/semmle/js/parser/JSDocParser.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/javascript/extractor/src/com/semmle/js/parser/JSDocParser.java b/javascript/extractor/src/com/semmle/js/parser/JSDocParser.java index 391f703d2b07..84027ced06c9 100644 --- a/javascript/extractor/src/com/semmle/js/parser/JSDocParser.java +++ b/javascript/extractor/src/com/semmle/js/parser/JSDocParser.java @@ -561,7 +561,8 @@ private Token scanNumber() throws ParseError { private Token scanTypeName() { char ch, ch2; - value = new String(Character.toChars(advance())); + StringBuilder sb = new StringBuilder(); + sb.append((char)advance()); while (index < endIndex && isTypeName(source.charAt(index))) { ch = source.charAt(index); if (ch == '.') { @@ -572,8 +573,9 @@ private Token scanTypeName() { } } } - value += new String(Character.toChars(advance())); + sb.append((char)advance()); } + value = sb.toString(); return Token.NAME; }