Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

C# Nuggets

JavaScript performance

Recently, the browser vendors have been making serious gains in JavaScript performance. A week ago, Google launched their web browser called Chrome with much fanfare. Chrome is based on the WebKit rendering engine which underpins Apple's Safari browser and the iPhone browser, but using a brand new Just-in-time (JIT) compiling JavaScript virtual machine. This JIT virtual machine is called V8 and was created over the past 18 months by a team led by Lars Bak who created the Java Hotspot virtual machine. The Chrome browser is Open Source in the form of code going by the name Chromium.

Interestingly, it turns out that Apple and Mozilla have spent the past few months building JITs for their JavaScript engines. Apple have an engine called JavaScriptCore, while Mozilla have an engine called SpiderMonkey. The JITs they have built are based on these existing engines but have been given new names. Apple's is called Squirrelfish-Extreme (SFX) while Mozilla's is called TraceMonkey.

Each of these engines seem to have unique features. V8 has what are called 'hidden class transitions' which are simply class definitions inferred at runtime. As class definitions change infrequently, this means that V8 has very fast class member lookup. TraceMonkey uses a novel technique called Tracing. Instead of compiling a method 'just in time' and then executing the compiled machine code, TraceMonkey interprets everything up until it detects a loop. At this point, it will compile the loop. So whereas traditional JIT compiling will compile code which potentially never gets run, Tracing will only ever compile code that gets run. Webkit's SFX has borrowed a trick from .Net and will compile any regular expressions into machine code. Their Webkit Regular Expression Compiler (WREC) is why SFX beats everyone else hands down on regular expression performance.

It has been a hectic 2 weeks. In that time, there have been various claims that first TraceMonkey was the fastest. Then V8 claimed they were the fastest. And over the past couple of days there have been claims that Webkit's SFX is the fastest. So who is right? Well, many factors are involved, and all three engines are still very much in active development. So the lead could shift around yet again.

To try and get a handle on the current state, I ran a benchmark called SunSpider. This is a JavaScript benchmark which does not test HTML DOM performance, therefore giving a true picture of pure JavaScript virtual machine speed.

The headline figure is:

Webkit (ie Safari) nightly builds are 46% faster than Firefox nightly builds. Chrome is 7% faster than Firefox.

However, the above statement does not give the whole picture. It appears that Safari's SFX JIT is not fully enabled on Windows. Recent Webkit checkins indicate that the Windows JIT is still being worked on. As Chrome is currently a Windows-only browser, it is impossible to compare Webkit-SFX with Chrome. Therefore, the 46% figure advantage Webkit has over firefox was obtained on OSX 10.5.

Microsoft have been working on a JavaScript JIT for a couple of years now. It's called Managed JScript and runs on top of the Dynamic Language Runtime (DLR). However, there has been no news from this project for a long while and it certainly won't be included in IE8. The old ActiveScripting-based JScript engine in IE8 has been tweaked a little for performance but lags a long way behind the other vendors. This is a real shame as Microsoft have some world-class virtual machine architects.

: Sunspider 0.9

Windows XP SP2 results (Core2 Duo, T7300/2Ghz)
Chromium: 2747.8ms +/- 2%
Firefox: 2941.4ms +/- 4.3% (1.07x as slow)
webkit: 3124.6ms +/-3.3% (1.14x as slow)

OSX 10.5.4 results (Core2 Duo, 2.8Ghz)
Firefox: 1361ms +/- 2.7%
webkit: 931ms +/- 2.3% (1.46x as fast)

Browsers used were the latest nightlies:

Win32 Firefox 3.1b1pre Gecko/20080910043000 (Content JIT=on)
OSX Firefox 3.1b1pre Gecko/20080910020330 (Content JIT=on)
Webkit r36309 using Squirrelfish-Extreme (SFX)

Published Wednesday, September 10, 2008 8:27 PM by rbirkby

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS



Websites tagged "compile" on Postsaver said:

June 12, 2009 9:17 AM

Leave a Comment

Enter the code you see below