Skip to content

Commit

Permalink
#499 Improve performance of HTML escaping by approx. 25%.
Browse files Browse the repository at this point in the history
Slow:
Average execution time: 0.00144 ms
Standard deviation: 0.059 ms

Fast:
Average execution time: 0.0018 ms
Standard deviation: 0.0014 ms
  • Loading branch information
yruslan committed Oct 18, 2024
1 parent 133d520 commit 72a9374
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,17 @@ object StringUtils {
* @return An escaped string.
*/
def escapeHTML(s: String): String = {
val out = new StringBuilder(Math.max(16, s.length))
for (i <- 0 until s.length) {
val out = new StringBuilder(Math.max(64, s.length))
var i = 0
while (i < s.length) {
val c = s.charAt(i)
if (c > 127 || c == '"' || c == '\'' || c == '<' || c == '>' || c == '&') {
out.append("&#")
out.append(c.toInt)
out.append(';')
}
else out.append(c)
i += 1
}
out.toString
}
Expand Down

0 comments on commit 72a9374

Please sign in to comment.