Slashdot Log In
Comparing Browser JavaScript Performance
Posted by
kdawson
on Thu Dec 20, 2007 12:45 PM
from the lining-'em-up dept.
from the lining-'em-up dept.
Thwomp writes "Over at Coding Horror Jeff Atwood has an interesting writeup on JavaScript performance in the big four browsers. He used WebKit's newly announced SunSpider to produce the results. If a probable anomaly in the IE7 results is overlooked, Firefox 2 is the slowest of the bunch. Atwood has also benchmarked the latest Firefox Beta, and its performance seems to be improved significantly."
Related Stories
This discussion has been archived.
No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Full
Abbreviated
Hidden
Loading... please wait.
A grain of salt (Score:5, Interesting)
Re:A grain of salt (Score:4, Insightful)
Parent
Re: (Score:3, Insightful)
I'd like to see those results.
Re:A grain of salt (Score:5, Insightful)
I guess there's value in such a benchmark, but it goes against the conventional wisdom of Amdahl's Law. If JS execution accounts for only 10% of the runtime of your application, it will be of little value to make JS 20% faster. You must concentrate on the other 90%.
Parent
Re: (Score:3, Insightful)
Hmmm (Score:5, Funny)
Like what? It didn't crash the system or it actually launched
Re:Hmmm (Score:5, Interesting)
Parent
Re: (Score:3, Insightful)
There could be a good reason for the test showing poor performance - say IE is shit at string concatenation - and then the result reflects badly on IE. Or, it could be that for whatever reason the benchmark hits som
Re: (Score:3, Interesting)
If that's true, (and I figure someone will calibrate me if I'm wrong) that could explain an apparent performance anomaly, if the browser farms out the string manipulations to another process.
The actual problem... (Score:5, Informative)
When you concatenate 2 strings in JScript, it determines the size of the buffer needed, allocates the buffer, does the concatenation, and returns the result to the caller.
Which works fine, until you start putting some load on those old tires...
x = 'some';
y = 'string';
z = 'here';
testString = x + " " + y + " " + z;
(Ignore that nobody would actually write code JUST LIKE THAT, they would just add a trailing or leading space to the x, y & z strings. But concatenating a result with some white space is not abnormal.)
Now, just think about what this is actually doing under the hood..
1. determine len of x (4) plus len of " " (1) = 5
2. Allocate new buffer(5)
3. Concatenate, assign result to temp
4. determine len of temp (5) plus len of y (6) = 11
5. Allocate new buffer(11)
6. Concatenate, assign results to new temp, destroy old temp
7. determine len of temp (11) plus len of " " (1) = 12
6. Allocate new buffer(12)
8. Concatenate, assign results to new temp, destroy old temp
9. determine len of temp (12) plus len of z (4) = 16
10. Allocate new buffer (16)
11. Concatenate, assign results to testString
So, to concatenate relatively little string data, 3 temporary buffers were needed and 4 separate allocations were done.
It works fine, right up to the point where it doesn't
Parent
Re:Hmmm (Score:4, Interesting)
4.5 HTML file, mainly consisting of one very large table. Took about 1 minute to render in IE, using about 80MB RAM. Was asked to lay it out in a more useful way. I went the fairly simple route - once it was fully loaded, I used JavaScript to lay it out better.
Original attempt was to just use innerHTML and string operations (couldn't remember the DOM commands, too lazy to look them up). The main bit was a single substring() on the div containing the large table (actually stripped off everything around the table).
The time to render the page jumped to 10 minutes! Memory use jumped sharply as well (largely because I was duplicating the table
For comparison, Safari 3 on Windows rendered the entire page in 5 seconds, whether I used innerHTML and substring() (including the duplication mentioned above) or DOM manipulation. Safari used a bit more memory than IE though (worst case 215MB compared to 200MB).
Parent
Re: (Score:2, Insightful)
Re:Hmmm (Score:5, Informative)
Fortunately, there are workarounds. The easiest is put all the component strings you wish to add together into an array and then call join('') on the array to build the result string. This executes much, much faster in IE and should be fast in the other browsers as well. Before I figured out the join('') "trick", I implemented a scheme to add the component strings in a much more optimal order...the N^2 cost is only if you add them in a naive, left-to-right manner. By pairing up adjacent components and adding, you get N/2 intermediate results. Repeating the pairing process on these intermediates, and over again until you have your 1 final result, the allocation cost is only N*Log(N), which is far FAR better than N*N. Incidentally, this is similar to the "Russian Peasant Algorithm" technique used to optimize the addition of N numbers on massively parallel processors...whereby you can reduce the cost of N additions from N to Log(N) time assuming there are enough processors (N/2) on hand. When I learned about the join(), I compared its performance to the Russian Peasant technique and got very comparable results in IE. Weired, since if join() were using a StringBuffer under the hood (like it should), then its performance should be O(N), not N*Log(N)...
Anyways, in summary - yeah it sucks hard that IE does that, but it's easy to workaround once you know what's up (gee, that's true in so many cases with IE quirks).
Parent
Re:Hmmm (Score:5, Insightful)
To paraphrase: "What surprised me here is that Firefox is substantially slower than IE, once you manipulate the experimental data by removing something that IE is particularly slow at."
And guess what? If you remove string ops, bit ops, and date ops, then Firefox is probably faster than IE.
Parent
Re:Hmmm (Score:5, Informative)
IE's string concatenation is ridiculously slow. I've found that in all browsers (except Firefox) it's faster to use an Array, push() together the string elements, and finally join('') them together to create the final string.
In Firefox, this is ever-so-slightly slower. In Internet Explorer, it's a good order of magnitude faster. (Depending on the length of the string. Concatenating a string 100 chars long together 10,000 times, I got a time of 13.7 seconds for plain old string concatenation ("string" + "string") versus .04 seconds for Array.push(). Firefox did the same test in .7 seconds using both methods.)
The SunSpider tests appear to be using plain-old string concatenation. I'll have to try rewriting them using Array.push() and see what the result is. Doing that IE's performance would almost certainly beat Firefox.
Parent
It's Flash (Score:5, Interesting)
I think Flash is the biggest DoS in the history of the web and Adobe really needs to take a good look at it. With all of these Flash ads and Flash-based video players, it really is a critical issue. Using adblock is an absolute must in my book, just to keep the browser running. My bet is some sort of resource leakage, since it happens over time -- like when watching several YouTube videos. It doesn't crash on the first or second one, but you're courting disaster on the third and above.
By the way, is there any way for FF to handle plugin crashes gracefully, i.e. *without* bringing down the browser with it? Maybe running it in a separate process somehow and just putting up a "broken image" sort of placeholder?
Parent
Re:It's Flash (Score:4, Funny)
If by fine you mean freaking bloody slow.
Flash designers wouldnt know speed if it slapped them with a large trout.
YouTube is good. It can handle a video pretty well (occasional frame dropping).
Ads and many other things will reduce the computer to a crawl instantly.
Parent
Ooh ooh let me guess (Score:5, Funny)
Re: (Score:3, Insightful)
Re: (Score:2, Offtopic)
Then whitelist sites you trust through site preferences.
Vista + ie7 took hours! (Score:5, Funny)
Opera (Score:3, Insightful)
Re: (Score:3, Informative)
There is an error in the aggregating mechanism of the benchmark. Otherwise, it would work in the current Opera as well.
Re: (Score:3, Funny)
Bah humbug! (Score:5, Informative)
First off IE6 is not tested, while it is still the most used browser. Also Opera 9.5 is not ready, it has so many bugs it just isn't funny anymore. Many sites break that worked fine in Opera 9.2.
But let's get to the good stuff. Yes in pure JavaScript like this, IE might be faster than Firefox. But in real world situations it clearly isn't. There is no test done on layout manipulation (and such) using JavaScript. Internet Explorer is notoriously horrible at this, especially if you use it combination with PNG's with alphachannel or complex CSS. Not to mention if you have to use JavaScript hacks because of IE6's lack of CSS support for the simplest things.
Funny is also how almost all JavaScript library speed tests I've seen put Internet Explorer far behind the others.
In 'real world' JavaScript performance Opera and Safari would be the winners, where I'd choose Safari as absolute winner, as Opera gains a lot of redrawing speed by cutting corners it should not cut which often result in display corruption (ie, the type that goes away when you force a redraw by minimizing and then maximizing the window again, or moving another window over Opera and then away again). Quite a bit behind in speed after Safari and Opera would be Firefox, followed even further back by Internet Explorer.
As a developer I still prefer Firefox for development though. Webkit is awesome, but the Safari GUI just plain sucks. Opera... hmm, I like it on my mobile devices, but it's just too weird for the desktop (what, textarea's don't even support scrolltop? wth!)
Re: (Score:2)
In 'real world' JavaScript performance Opera and Safari would be the winners, where I'd choose Safari as absolute winner, as Opera gains a lot of redrawing speed by cutting corners it should not cut which often result in display corruption (ie, the type that goes away when you force a redraw by minimizing and then maximizing the window again, or moving another window over Opera and then away again). Quite a bit behind in speed after Safari and Opera would be Firefox, followed even further back by Internet Explorer.
I was wondering about that, a lot of the things that were listed as being a part of the benchmark aren't thing which I've ever used.
It looks to me like the benchmark was put together in a way that favors obscure optimizations over genuine real world use. 3d isn't something that I run into a whole lot on the web.
This looks less like performance issues and more like decisions made on the part of the developers as to what is more important than what.
Personally I'd be more focused on things like the regexes, s
Re:Bah humbug! (Score:5, Insightful)
Parent
Some more data ... (Score:3, Informative)
AMD Athlon 64 3200+, 1GB, Ubuntu 7.04, using FF for 4 hours, listening to last.fm.
CC.
Re: (Score:2)
Firefox through Wine (Score:3, Interesting)
Wine results [webkit.org]. Native results [webkit.org].
Re: (Score:2)
client side javascript will become our enemy (Score:3, Insightful)
My basic rule of thumb has also been that client scripting should enhance and application but not be required for the application. In other words with JavaScript disabled the application might act rudimentary but will still produce results.
Re:client side javascript will become our enemy (Score:4, Informative)
Compare it to the days of IE5.5 vs Netscape 6 (the worst browser ever released) vs Netscape 4.7 and you can see what huge progress has been made.
Parent
Re: (Score:3, Informative)
I can second this statement. With libraries like prototype, and others that build on top of it, cross-browser development is simplified. The libraries take care of many of the differences between browsers, leaving the developer to work on the actual features.
Re:client side javascript will become our enemy (Score:4, Insightful)
Parent
Re: (Score:2)
Re: (Score:3, Insightful)
Really, what's next, catering to people who don't have CSS?
That's exactly why CSS exists... to separate content from layout/display characteristics. You can make an entirely graphical-button image-chopped page with all kinds of rollover goodness, and as long as you write your (x)html and CSS properly, it will break down fine in a text or mobile browser. Using techniques like image replacement is critical if you want your flashy website to be accessible to people using screen-readers or mobile browsers.
This of course assumes that your site has some kind of co
Same test for OSX, please? (Score:2)
better idea (Score:2, Insightful)
lynx beats them all, 17 ms to pass test (Score:3, Funny)
~$ time lynx -dump http://webkit.org/perf/sunspider-0.9/sunspider-driver.html [webkit.org]
SunSpider JavaScript Benchmark (In Progress...)
real 0m0.017s
user 0m0.001s
sys 0m0.004s
~$
Re: (Score:3)
Now We'll See Performance Improve (Score:2)
The JIT compiler isn't in JavaScript yet. (Score:5, Interesting)
FireFox was supposed to be getting a JIT compiler for JavaScript. [mozilla.org] It's the one from the Flash player, where it runs ActionScript. That's apparently now expected in 2008. Then we'll see some real improvement.
Just a note: it wasn't "supposed" (Score:2, Insightful)
Konqueror (Score:2)
It would be cool (Score:2)
Good one (Score:3, Insightful)
Had it been "the big one and the one that just edged into relevance", then most people would know what you meant.
And the winner: Linux (Score:5, Informative)
WinXP Firefox 2.0.0.11: 18.8s
WinXP Internet Explorer 7.0.5730.11: 33.1s (same issue with the strings performance)
Ubuntu Firefox 2.0.0.6 (yeah, well): 15.3s
I only have opera running on my WinMobile Dell Axim v51x PDA and it's currently running, it seems to be 30-40 times slower than the desktop, so I'll not be waiting before I post...
Re: (Score:3, Funny)
Okay, it wasn't that bad - it finished in 317.8s. So 15-20 times slower. I was actually surpriced that it managed to run to the end.
Compare JavaScript Frameworks (Score:4, Interesting)
If you run the MooTools Slickspeed tests [mootools.net] in different browsers, you find something interesting:
jQuery also claims to be the most accurate, though who knows for sure.