Simple JavaScript/AJAX Optimizations
Often people encounter performance problems in largish JavaScript/AJAX applications and just write it off as a hazard of the language. There are several techniques, however, which can alleviate a lot of common bottle-necks. Depending on the situation you may not be able to take advantage of all of them, but any one of them can be a huge help.
My Background: I use JavaScript extensively in server-side processes and workstation tasks on Windows with Windows Scripting Hosting (and Microsoft's JScript implementation), so I naturally know more about the short comings of that environment as opposed to SpiderMonkey/FireFox.
Use Array for large/long string building:
If you're a .NET developer you're familiar with the StringBuilder which you know to use instead of adding strings together with the plus sign. In JavaScript I encourage you to do something similar and push the strings to anArray, then join the elements at the end. JScript/Internet Explorer users will thank you, because Microsoft's implementation is ungodly slow (SpiderMonkey/FireFox users will be delighted that plus-sign concatentation is actually faster by a small margin). In Windows with JScript, every time you concatenate a string to another string it will allocate a third string, copy the contents of both source strings into it, and then destroy the old string. In long loops, this can get so slow that a web browser or command script will completely hang near-indefinitely.
For small strings, especially those not in a loop of some kind, using plus-sign concatenation is just fine. Maybe some day this won't matter, because JScript/Internet Explorer will get fixed!
Use charCodeAt() instead of charAt():
The best place to put this into practice is anytime you're comparing individual characters, not appending them to a string. The difference being that you're getting back a Number (the character code) rather than a String(the character itself). JavaScript has no single-character type (yes it does have types underneath its sexy var's).
In the JScript implementation on Windows this must not only allocate a VARIANT, but also a BSTR.
Use RegExp::exec instead of String::match:
There's good and bad to this. The good is that you create a single RegExp object and subsequent uses don't incur regular expression compilation/syntax checks (assuming, of course, that you're not creating the RegExpobject in your loop or function ... you aren't are you?). The bad is that you can't use the nifty global (g) flag; exec will only find the first match and stop. This makes it a wee bit faster, but that doesn't matter if you need to find N matches.
My Background: I use JavaScript extensively in server-side processes and workstation tasks on Windows with Windows Scripting Hosting (and Microsoft's JScript implementation), so I naturally know more about the short comings of that environment as opposed to SpiderMonkey/FireFox.
Use Array for large/long string building:
If you're a .NET developer you're familiar with the StringBuilder which you know to use instead of adding strings together with the plus sign. In JavaScript I encourage you to do something similar and push the strings to anArray, then join the elements at the end. JScript/Internet Explorer users will thank you, because Microsoft's implementation is ungodly slow (SpiderMonkey/FireFox users will be delighted that plus-sign concatentation is actually faster by a small margin). In Windows with JScript, every time you concatenate a string to another string it will allocate a third string, copy the contents of both source strings into it, and then destroy the old string. In long loops, this can get so slow that a web browser or command script will completely hang near-indefinitely.
For small strings, especially those not in a loop of some kind, using plus-sign concatenation is just fine. Maybe some day this won't matter, because JScript/Internet Explorer will get fixed!
Use charCodeAt() instead of charAt():
The best place to put this into practice is anytime you're comparing individual characters, not appending them to a string. The difference being that you're getting back a Number (the character code) rather than a String(the character itself). JavaScript has no single-character type (yes it does have types underneath its sexy var's).
In the JScript implementation on Windows this must not only allocate a VARIANT, but also a BSTR.
Use RegExp::exec instead of String::match:
There's good and bad to this. The good is that you create a single RegExp object and subsequent uses don't incur regular expression compilation/syntax checks (assuming, of course, that you're not creating the RegExpobject in your loop or function ... you aren't are you?). The bad is that you can't use the nifty global (g) flag; exec will only find the first match and stop. This makes it a wee bit faster, but that doesn't matter if you need to find N matches.