PDA

View Full Version : [2.2] slow String concatenation



izso88
17 Mar 2009, 5:04 AM
I am not sure this is a bug, but maybe you can help me out here.:)

I have been developing some JavaScript classes for charting purposes, and when I ran them on Ext I felt like they perform slower. I will cut the detective story part, and will get directly to the facts. String concatenation is mind-blowingly slow in Ext.
Try it for yourself:


s=new String();
for (i=0; i<10000; i++) {
s=s.concat("i="+i+",");
}

On Ext this piece of code runs for more than 10 seconds, while if Ext is not used, it takes half-a-second or so.

Is it possible to bypass the Ext's modified String Object, or tho disable it fully?

Thank you.

Animal
17 Mar 2009, 5:41 AM
This is not an Ext issue.

Strings are Strings.

Condor
17 Mar 2009, 5:45 AM
Could this have something to do with the fact that Ext extends the String prototype?

ps. Creating the string from a joined array is probably a lot faster, e.g.

s = [];
for(var i = 0; i < 10000; i++) {
s.push(i);
}
s = 'i=' + s.join(',i=');

Animal
17 Mar 2009, 5:46 AM
I can run this



v=new Date().valueOf();s=new String();for (i=0; i<10000; i++) { s=s.concat("i="+i+",");}console.log(s.length + " chars took " + (new Date().valueOf() - v) + " millisecs")


On the Firebug console command line, and it consistenly takes 29 milliseconds whether it's in a page with Ext or not.

Condor
17 Mar 2009, 5:48 AM
Yes, but you're completely ignoring IE here! Have you tried the same on IE?

ps. You could still be correct, I didn't check... You need to test a real IE6 or 7, because IE8 in IE7 emulation mode still uses the IE8 javascript engine.

Animal
17 Mar 2009, 5:48 AM
Could this have something to do with the fact that Ext extends the String prototype?

I can't see how it could. Adding properties to an object shouldn't affect the speed of execution.

Could it be slowing down the access to the concat property of the prototype? concat is looked up every time.

This must be browser-specific. Which browser exhibits this?

izso88
17 Mar 2009, 5:51 AM
The problem seems to persist only in IE6+...

It is the same for +=, and for concat.
Will try the idea with an array...

Animal
17 Mar 2009, 5:54 AM
AGH!

33 seconds on IE6!

izso88
17 Mar 2009, 5:59 AM
The Array-trick works, and solves my problem. :D
Thanks!

However the += operation is abnormally slow on IE...

Animal
17 Mar 2009, 6:00 AM
As Condor pointed out, it's not a good way of creating a String.

Each time, you are creating a new String object, assigning it to the variable, and throwing away the old one.

Animal
17 Mar 2009, 6:01 AM
But is that abnormal on IE?

The thing is a mess.

jay@moduscreate.com
17 Mar 2009, 6:18 AM
The thing is a mess.

What is not a mess from the MichaelSoft camp?

http://webpages.csus.edu/~av289/hiding1.jpg

mschwartz
17 Mar 2009, 6:25 AM
AGH!

33 seconds on IE6!

33 seconds? May as well send a request to the server and let it do the work.

EDIT:

Better yet:



if (Ext.isIE6) {
top.location.href = 'http://getfirefox.com';
}

jay@moduscreate.com
17 Mar 2009, 6:28 AM
why use concat? why not:


s=new String();
for (i=0; i<10000; i++) {
s += "i=" + i +",";
}