Java escape HTML - string replace slow?

Posted by cpf on Stack Overflow See other posts from Stack Overflow or by cpf
Published on 2010-04-06T13:11:26Z Indexed on 2010/04/06 13:23 UTC
Read the original article Hit count: 212

Filed under:

Hi StackOverflow,

I have a Java application that makes heavy use of a large file, to read, process and give through to SolrEmbeddedServer (http://lucene.apache.org/solr/).

One of the functions does basic HTML escaping:

private String htmlEscape(String input)
{
    return input.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
        .replace("'", "&apos;").replaceAll("\"", "&quot;");
}

While profiling the application, the program spends roughly 58% of the time in this function, a total of 47% in replace, and 11% in replaceAll.

Now, is the Java replace that slow, or am I on the right path and should I consider the program efficient enough to have its bottleneck in Java and not in my code? (Or am I replacing wrong?)

Thanks in advance!

© Stack Overflow or respective owner

Related posts about java