Welcome GuestLogin

A Drunken Wiki


A Drunken Wiki is the companion wiki to the blog "A Drunken Madman", written by Jason Brown.


Search the wiki

Vaccination Saves Lives: Stop The Australian Vaccination Network

Find me on Twitter, Facebook, Google+, Pinterest and LinkedIn

Pin It

Alpha Archive Atheism Blue Mountains Climbing Cycling Development DotNet GPS Guitar HMHB JavaScript JQuery LINQ Meta MTB Music Perl Powershell RNP SharePoint Skepticism Social Media Training Ukulele Vaccines Visual Studio WebParts Woodford Festival 2011 XML


Faster String Concatenation with ADODB.Stream (JScript)

Modified on 2012/01/14 12:00 by Administrator Categorized as Development
This article was first published back in 2003-2004 at InfiniteMonkeys Web programming Resource. It applies to ASP3.0 specifically, but applies to anything using the JScript engine, such as Windows Script Host scripts or similar

Faster String Concatenation with ADODB.Stream
by : Atrax

String concatenation is a surprisingly expensive operation in terms of memory and processing. This article shows a little trick you can use to speed up and streamline your concatenation-centric code.

String concatenation is a killer. It may seem like a relatively simple little operation, but the fact is, joining strings is very expensive because of the way the script engines handle it. VBScript is especially bad in this regard, taking a surprisingly large amount of time to join strings together. ASP.NET has a built-in class to try and get around this (StringBuilder), but classic ASP doesn't have anything similar. Or does it?

In fact, there are a number of ways you can drastically cut the penalty of building strings. I knocked together a little test of three methods, the results of which may surprise you. I've already introduced by simple benchmarking code in another article, so I won't explain that here. What I basically did was, within my benchmark code, looped over each concatenation method 30,000 times (in order to magnify the differences and therefore make them more clearly measurable). The test were carried out on my laptop, a Dell Inspiron 8600 with a 1.6GHz processor on a Centrino chipset with 1Gb RAM running Windows Server 2003, Windows Script 5.6 and ADO 2.8. The code is in JScript, which has already been shown to be faster than VBScript at concatenation.

Method 1. plain string concatenation

var buffer = "";
for(var x=0;x<30000;x++)	{
	buffer += "**";
}	Response.Write(buffer)

Simple, right? well, running this code on this processor took, on average, around 3.7 seconds. That's a long time. sure, it's 30,000 iterations, but it's still not great, especially when compared to method #2

Method 2. A trick with Arrays

var buffer = new Array(); var y = 0;
for(var x=0;x<30000;x++)	{
	buffer[y++] = "**";
}	var x = buffer.join(''); Response.Write(x);

This one was much better. On average, this one ran at around 331 milliseconds. That's over ten times the performance of the plain concatenation method. This is good, very good, but I also tested a third method, using Direct Output to Response.Write.

Method 3. Direct Output

var buffer = "";
for(var x=0;x<30000;x++)	{

This one took about 210 milliseconds on my system, or thereabouts. It's a better option than joining together and Response.Write()ing, but of course there are times it can't be used, such as when you're building a string for another use than direct output, so in those cases, I'd use the following method.

Method 4. ADODB.Stream

var buffer = new ActiveXObject("ADODB.Stream");
for(var x=0;x<30000;x++)	{
}	buffer.position = 0; var x = buffer.readText()

It looks like more code, of course, but that's an illusion. This code averaged at around 180 milliseconds. That's nearly twice as fast as the Array trick, and twenty, yes twenty times the speed of the plain vanilla concatenation method. This is a phenomenal saving, and well worth taking the time to implement.

So what should you use? well, isn't it obvious? go with the stream if you need to do some serious string joining (as I had to recently with a CSV parser I built, whose save() method used a fair whack of concatenation). It could save you some serious processor cycles, and as we all know, that's very good for your application, though if you're only doing a little joining, the saving will be less marked, due to the COM creation phase. If you'd like to play with this yourself, the simple test code is here (link lost)


I wrote this a long time ago when ASP3.0 was still a very viable and commonly-used server-side framework. These days it's fairly rare to see new work done in ASP - with the rise of Ajax and the richer class library offered by ASP.NET, there's less call for ASP - and therefore JScript and VBScript on the server. Still, the lessons hold true. Joining strings is expensive in terms of memory and cycles. In a .NET context, this problem was catered to from the beginning, with the StringBuilder class. This is something that should always be used where hectic string manipulation happens. For the most part, the Array joining trick above will be faster than string manipulation and while you may think saving one or two cycles here and there is of limited value, scale it up to, say, 5000 page impressions, or 10,000, or more.

Once scaling comes into the picture, the laws of large numbers start to take effect. One step is one step, but a journey of a thousand miles is made up of many small steps. Think where you can save cycles, do it, save the world.


ScrewTurn Wiki version Some of the icons created by FamFamFam.