Slow javascript performance in IE (string concatenation)

I’ve come to realize while IE 7 is no less than a landslide improvement over IE 6, it is still IE. While IE alone in the market appeared to be a superior product, compared to the likes of firefox it is simply inferior. Firefox script interpreter is all around faster, in my experience it is not just 2-5x faster, it can be 100x for some operations. Crazy right? I’ve been pushing the envelope with an ajax/dojo based site I’ve spent a large amount of time on. To be blunt there is more than one occasion during the development process I have had to rewrite code to accomadate the script execution of IE 7. Looking back most of the time the script was poorly thought out set of operations that needed to be rewritten anyway.

My last road block however was a particular chunk of JSON data that was packaged up base64 encoded then sent off to the server via the dojo xhrpost method. I really was scratching my head whenever I was in IE and got up to a 3-10 second delay while sending this data to the server. While in Firefox and Safari there was none. After a period of a few hours I narrowed the delay to my base64 encoding script running on the client side causing the delay. It didn’t take long start finding that IE is incredibly inefficient as string concatenation. The solution is simple.  Implement a string buffer.  I have provided some snippets along with the corrected JS file for viewing.

// addresses some speed issues with IE 
function StringBuffer()
{
this.buffer = [];
}
 
StringBuffer.prototype.append = function append(string) {
this.buffer.push(string);
return this;
};
 
StringBuffer.prototype.toString = function toString() {
return this.buffer.join("");
};
 
// end buffer
 
function encodeBase64(str){
 
...
 
var buf = new StringBuffer(); // new buffer code
 
...
 
//result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
 
buf.append(base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
 
...
 
}

Source for the entire base64 file here:
base64.js

Be Sociable, Share!

Discussion

  1. Drew Diller says:

    You sir, are the savior of my evening!

    Found some code a (very smart) coworker wrote – it was bog slow in IE7. The funny part is that the method names were the exact same as your example, I just had to change a few things around.

    I owe you a beer, or lunch, or something.

  2. alixchap says:

    I really have to thank you for sharing your code.

    I have an application that uses a lot of JavaScript and I experienced performance issues in IE7.

    I spent several days reviewing hundreds of lines of code but I couldn’t figure out the root cause. Using the approach that you describe in this post solved my problem.

    I also feel that I owe you a beer or something .. Count on it if you visit Mexico.

    Regards,

Add a Comment

*