You are right, Gwt is optimized to perform the best in each case. Both StringBuilder and StringBuffer generates the same javascript code.
I have coded a simple test (see below) to check append and these are my results (Compiled code): - all browsers except IE performs identical using String, StringB. - IE6 performs better using StringB. - IE8 performs better using normal String. In hosted mode, the performance is better for StringB Cheers -Manolo public void testStringOperations() { int TIME = 20000; String DATA = "sadfasdf"; String s = ""; double ss = new Date().getTime(); for (int i=0; i<TIME; i++) { s += DATA; } double se = new Date().getTime(); String a = ""; double as = new Date().getTime(); for (int i=0; i<TIME; i++) { a = a + DATA; } double ae = new Date().getTime(); double bs = new Date().getTime(); StringBuffer b = new StringBuffer(); for (int i=0; i<TIME; i++) { b.append(DATA); } double be = new Date().getTime(); double us = new Date().getTime(); StringBuilder u = new StringBuilder(); for (int i=0; i<TIME; i++) { u.append(DATA); } double ue = new Date().getTime(); RootPanel.get().add(new Label(se - ss + " " + s.length() + " String += ")); RootPanel.get().add(new Label(ae - as + " " + a.length() + " String = String + ")); RootPanel.get().add(new Label(be - bs + " " + b.length() + " StringBuffer" )); RootPanel.get().add(new Label(ue - us + " " + u.length() + " StringBuilder" )); } On Tue, Jul 13, 2010 at 11:41 AM, Thomas Broyer <t.bro...@gmail.com> wrote: > > > On 13 juil, 11:10, Manuel Carrasco Moñino <man...@apache.org> wrote: >> I've not checked the code produced by the Gwt compiler, but I think it >> could introduce a bit of overhead or may be none (if the >> implementation code was 100% optimal). >> Anyway the use of String should guarantee that the final code is >> faster or equal. > > That's wrong. StringBuilder will use either string concatenation (in > which case you'll suffer a very small, and negligible, penalty at > runtime) or array-append-then-join, depending on which method was > benchmarked the fastest in each browser. > http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/super/com/google/gwt/emul/EmulationWithUserAgent.gwt.xml > The GWT has experimented with many options before settling on these > two: using a.push() or a[arrayLength++] to append to an array, using > join() or String.prototype.concat.apply() to join the array items into > a string, etc. you'll find them all in the subversion repository: > http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/core/client/impl/ > Note that string concatenation is done using the += operator, which is > more performant than s = s + something in some browsers (AFAIK). > > So really, GWT is optimized here, and you shouldn't fear from using > StringBuilder (or StringBuffer) if you think they're necessary (of > course, when concatenating 3 strings, it might not be worth it; > StringBuilder is mostly useful with loops and/or building large > strings, and this is true in both Java and GWT) > > -- > You received this message because you are subscribed to the Google Groups > "Google Web Toolkit" group. > To post to this group, send email to google-web-tool...@googlegroups.com. > To unsubscribe from this group, send email to > google-web-toolkit+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-web-toolkit?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.