Microsoft KB Archive/306823

= HOW TO: Improve String Concatenation Performance in JScript .NET =

Article ID: 306823

Article Last Modified on 6/18/2003

-

APPLIES TO


 * Microsoft JScript .NET
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2

-



This article was previously published under Q306823





For a Microsoft Visual Basic .NET version of this article, see 306821.



For a Microsoft Visual C# .NET version of this article, see 306822.



IN THIS TASK

 * SUMMARY
 * Description of Strings in the .NET Framework
 * Create a Test Application
 * Compile the Application
 * Test the application
 * Troubleshoot
 * REFERENCES



SUMMARY
This step-by-step article describes how to improve performance of string concatenation by using the StringBuilder class instead of conventional concatenation techniques.

back to the top

Description of Strings in the .NET Framework
When strings are being concatenated, the + concatenation operator builds a new string each time. A large amount of string concatenation that uses the + operator may affect performance. However, the .NET Framework includes a StringBuilder class that is optimized for string concatenation and is much faster and more efficient than the + operator while concatenating multiple strings.

The StringBuilder class can allocate more memory as required to store characters when the value of an instance is enlarged. The capacity is adjusted automatically. When an instance of StringBuilder is initialized, a default capacity is used if the capacity is not specified. This improves performance in string concatenation.

The sample application in this article demonstrates how to use the StringBuilder class, and compares the performance to conventional concatenation.

back to the top

Create a Test Application
 Create a folder for your application. Name the folder C:\Testfolder .  In a text editor (such as Notepad), paste the following Jscript .NET code, and then save the file as C:\Testfolder\ sbConcat.js : import System; const sLen:int = 30, Loops:int = 5000; var sTime:DateTime, eTime:DateTime; var i:int; var sSource:String = new System.String('X', sLen); var sDest:String = &quot;&quot;; // // Time string concatenation // sTime = DateTime.Now; for(i=0;i

back to the top

Compile the Application
 Start Microsoft Visual Studio .NET. In Visual Studio .NET, type the following command at a command prompt, and then press ENTER:

cd C:\testfolder

</li> To compile the application, type the following command, and then press ENTER:

jsc /target:exe sbConcat.js

Note The sbContact.exe file is created in the C:\Testfolder folder.</li></ol>

back to the top

Test the Application
To test the application, type the following command, and then press ENTER:

sbConcat

You may receive the following application output in the console:

<pre class="fixed_text">Concatenation took 10.65625 seconds. String Builder took 0 seconds.

back to the top

Troubleshoot

 * If your computer is in an environment that supports streaming the data (such as in an ASPX Web Form), or your application is writing the data to a hard disk, you can avoid the buffer overhead of concatenation or the StringBuilder class by writing the data directly to the stream. To do this, you can use the Response.Write method, or use any method that is appropriate for the stream that you are writing to.
 * Try to reuse the existing StringBuilder, instead of reallocating each time that you require one. If you reuse the StringBuilder, you might avoid growing the heap and triggering unnecessary garbage collections. In either case, if you use the StringBuilder, you use the heap more efficiently than if you use the + concatenation operator.

back to the top

<div class="references_section">