function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
richardvrichardv 

StringBuffer equivalent

Is there a StringBuffer equivalent for Apex?  I don't see one in the API. 

If there is no equivalent, could someone on the Apex Platform team shed some light on performance impacts of using + to append strings? 
JeremyKraybillJeremyKraybill
How many "+" operations are you planning on doing in a single request? Given the speed of VM's these days, and the power of the Salesforce platform, I can't see how any number of "+" operations would visibly affect performance of a VisualForce app.

Jeremy Kraybill
Austin, TX
richardvrichardv
Hi Jeremy, thanks for the feedback.  In my specific use case, I can't answer how many because it varies but for the sake of argument let's say its five thousand.  I gotta believe that an API specifically designed to handle String concatenation would work exponentially better than the + sign given my knowledge of how Java (what I believe salesforce runs under the covers) handles String concatenation.  Since plenty of people have made the String vs StringBuffer argument, here is a good one: http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html.  I encourage you to read in order to understand my concern.

Can someone from the force.com development team please speak up on this?  I was at Dreamforce and all of you said you monitor the forums - please prove it!
JeremyKraybillJeremyKraybill
As a Java guy myself I definitely understand the problem. Back in 2000 it was probably 100x worse than it is today, due to modern VM's having much cheaper object creation. It still is probably 10-100x slower to use += than it is to use a StringBuilder/StringBuffer in Java.

But regardless, you should run some tests and look at how much latency you are talking about. Even if you really have to do 5000 concatenations in VF, I ran a quick 3-line test in sandbox and was able to do 5000 concats in 2-3 seconds. This compares to about 5 seconds in Java on my local machine for the same operations. I really think you are fine.

I would encourage you to open an IdeaExchange idea to expose StringBuffer, since it should be very easy to do this based on what I know of the Apex infrastructure.

Jeremy Kraybill
Austin, TX