a life of coding

Wednesday, December 10, 2008

JavaScript Performance

There are a lot of JavaScript performance benchmarks flying around out there. Some claim that Chrome shames the competition, others say that Firefox 3.1 is neck and neck with Chrome, others crown WebKit as the fastest of them all.

But what they never seem to do is compare them against other languages. One website, the Computer Language Shootout does, but it never seems to rate JavaScript particularly well. There are a few reasons for this. JavaScript has gotten a lot of attention recently and has thus been rapidly improving, and the CLS doesn't update very often. Tests are also written by different people, and folks who write JavaScript have never been the performance oriented crowd. Finally, the command line harness they use to execute JavaScript isn't representative of how users execute JavaScript.

So, what happens if we try to mitigate some of these? I picked a single test, rewrote it to run as a web page, and ran it using the latest version of browsers that can be considered stable. The results might surprise you.


n = 10
gcc0.5s1.0x
java60.7s1.4x
java51.1s2.2x
webkit1.8s3.6x
jsc4.7s9.4x
minefield6.4s12.8x
firefox19.4s38.8x
rhino22.1s44.2x
python33.1s66.2x
rhino47.8s95.6x
ruby58.3s116.6x
webkit [parallels/xp]1.0s
chrome [parallels/xp]2.0s

n = 11
gcc5.1s1.0x
java66.5s1.3x
java512.6s2.5x
webkit23.4s4.6x
jsc57.5s11.3x
minefield81.2s15.9x
webkit[parallels/xp]15.0s
chrome[parallels/xp]29.0s

Timings done on a Mac Book Pro 2.4 Gz Core 2 Duo / 4 GB RAM. Parallels/XP means running on Windows XP SP3 inside Parallels on the same computer.
WebKit is nightly build 39090.
WebKit [parallels/xp] is nightly build 39088.
jsc is built from svn rev 39090.
python is 2.5.1.
ruby is 1.8.6.

So, interesting things:

  1. The fastest JavaScript implementations are close to the speed of Java.

  2. The coming JavaScript implementations are substantially faster than Python or Ruby.

  3. WebKit is about twice as fast as jsc (the command line interpreter), so the Computer Language Shootout numbers will be inflated.

  4. Java 6 is substantially faster than Java 5.

So, I expect to see more web services written in JavaScript. Why? The argument for Python or Ruby has been that they are much more productive than Java/C/C++/C# that the performance of these languages isn't important. I certainly agree with this. However, JavaScript has about the same level of language productivity, and now has an implementation thats 18 times as fast as python and 32 times as fast as ruby. And you can use the same language across the board for web apps.

I also expect to start seeing desktop apps written in JavaScript. Why use a hacky python or ruby desktop app wrapper when you could use the best cross platform GUI kit there is? And, why bother with a local rails or django instance when you could do everything in a full MVC AJAX kit like Sproutcore or Objective-J?

At the risk of giving away the secrets to my sauce, desktop JavaScript is going to change everything. There are already a couple of frameworks out there (Jaxer, Titanium), but I think that there will be many more to come.

(PS: Lisp FTW!)