Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

The Hidden Evils Of Java String Methods

DZone's Guide to

The Hidden Evils Of Java String Methods

· Java Zone ·
Free Resource

FlexNet Code Aware, a free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and open source license compliance issues.

If you code in Java, you have inevitably used the String.split() and String.replace() (including replaceFirst() and replaceAll()) functions.

And why wouldn't you? They are much more convenient than using the Java Regular Expressions API where you need to create a 'Pattern' object, and possibly a 'Matcher', and then call methods on those.

However, all convenience comes at a price!

The Evil Inside

In this case, the String.split() and String.replace*() methods (with the sole exception of String.replace(char, char) ) internally use the regular expression apis themselves, which can result in performance issues for your application.

Here is the String.split() method:

public String[] split(String regex, int limit) {
      return Pattern.compile(regex).split(this, limit);
}

Notice that each call to String.split() creates and compiles a new Pattern object. The same is true for the String.replace() methods. This compiling of a pattern each time can cause performance issues in your program if you call the split() or replace() functions in a tight loop.

Benchmark

I tried a very simple test case to see how much the performance is affected. 

The first case used String.split() a million times:

public static void main(String[] args) 
{
	long begin = System.nanoTime();
	
	ArrayList<String[]> list = new ArrayList<String[]>(1000000);
	for (int i = 0; i < 1000000; i++)
	{
	  String[] split = "Hello World".split(" ");
	  list.add(split);
	}

	long end = System.nanoTime();
	System.out.println(list.size());

	System.out.println(TimeUnit.MILLISECONDS.convert(end-begin,TimeUnit.NANOSECONDS));
}

In the second case, I just changed the loop to use a precompiled Pattern object:

//create the Pattern object outside the loop	
Pattern pattern = Pattern.compile(" ");

for (int i = 0; i < 1000000; i++)
{
	String[] split = pattern.split("Hello World", 0);
	list.add(split);
}

Benchmark Results

Here are the average results of 6 test runs:

Time taken with String.split() : 1600ms
Time taken with precompiled Pattern object: 1195 ms

Split-benchmark

Conclusion

Note that I used an extremely simple regular expression here which consists of just a single 'space' character and it resulted in > 25% decrease in performance.

A longer more complex expression would take longer to compile and thus make the loop containing the split() method even slower compared to its counterpart.

Lesson learned: It is good to know the internals of the APIs you use. Sometimes the convenience comes at the price of a hidden evil which may come to bite you when you are not looking.

 

From http://eblog.chrononsystems.com/hidden-evils-of-javas-stringsplit-and-stringr

 Scan Java, NuGet, and NPM packages for open source security and license compliance issues. 

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}