I had to choose a way of efficient string string concatenation for GWT application. For this I did a small test and thought it will be helpful for others to know results as well.
So, surprisingly difference is quite minor: ~100ms for 1000000 concatenations. So, please choose appropriate from code reading point of view.
My testing was simple:
// + operator
private void str() {
Date start = new Date();
String out = "";
for(int a=0;a<1000000;a++) {
out += "item" + a;
}
Date end = new Date();
MessageBar.error("str:" + (end.getTime() - start.getTime()));
}
// StringBuffer implementation
private void sb() {
Date start = new Date();
StringBuffer out = new StringBuffer();
for(int a=0;a<1000000;a++) {
out.append("item" + a);
}
Date end = new Date();
MessageBar.error("sb:" + (end.getTime() - start.getTime()));
}
Results were:
str:1612
str:1788
str:1579
sb:1765
sb:1818
sb:1839
Stringis immutable. Java has to create a newStringobject each time.