views:

151

answers:

2

Hey guys,

So we have this product, and it's really slow in IE. We've already applied a lot of the practices advised by the IE guys themselves (like this, and this), and try to sacrifice clean code for performance in the critical parts like DOM manipulation.

However, as you can see in this IE profiler screenshot.. alt text

Just "String" is the biggest offender. Almost 750ms of exclusive time.

Does this mean IE is spending 750ms just instantiating Strings? I also read this stuff on the Opera dev blog:

A build script can remove whitespace, comments, replace strings with Array lookups (to avoid MSIE creating a string object for every single instance of a string — even in conditions)

But no more info regarding this. Anyone can clarify? It seems like IE has to create a full String instance every time you have " " in your code, which could explain this, but I don't know what the array lookup optimization would look like. BTW- we don't really do much of string concatenation anywhere in the code.

The library we use is MooTools 1.2.4

Any suggestions will be appreciated! Thx

UPDATE- I'm particularly interested in the tip mentioned above about "array lookup optimization". Our library is big (1MB) so it has a lot of strings in it, like any other JS code. But since our library is bigger than most, these Strings are actually causing speed issues.

Also, does anybody know if adding stuff to the String.prototype makes every instance slower?

+4  A: 

I'd grab a profiler that will give you a deeper view, you can see exactly what about String is taking so long. For IE specifically there's dynaTrace AJAX Edition (yes, it's free).

I'd fire up your same pages in there, it'll give you a tree breakdown so you can see what's going on...along with a hot spots view of exactly what low-level functions are taking the longest.

Nick Craver
Seconded - dynaTrace is so awesome it's almost weird
Pointy
dynaTrace looks pretty awesome, but our product doesn't work when it's running. No errors or anything, it just doesn't work. Sucks. But I already know which low-level function is causing most of the performance problem, and it's `String`. So what I'm asking is if anybody knows how to optimize the use of strings
Infinity
+1 for dynaTrace. Never heard of it before.
Alec Smart
@Infinity - Very weird, might want to ask the dynaTrace guys, they're pretty good at fixing things. What you know is that string is causing the error...what dynaTrace will tell you is the function that's calling it 2000 times, and the one calling that, etc...that's often the issue. Getting a call tree instead of just hits per function is **tremendously** useful in my experience, I really hope you're able to get it running...it's an unparalleled JS debugging tool in the IE world.
Nick Craver
A: 

Strings are immutable in Javascript. Meaning when you do something like this:

alert("hello" + " world");

three strings are being created:

  1. hello
  2. word
  3. hello world

Finding such instances and fixing those can be helpful. Like Nick said, using a profiler to pin down exactly what specific code with Strings is causing trouble is likely the best way to go.

Anurag
There's no specific part of the code that's doing anything crazy with strings, most libraries have thousands of strings in the source code, but when you have a library that's 1 MB, then those become a problem. I was particularly interested in the tip the Opera devs mentioned in their blog
Infinity