JavaScript Is Slow
Written by Harry Fairhead   
Saturday, 17 December 2011

The battle lines are drawn up - JavaScript v Native. Some programmers are of the opinion that only Native code will do and C++ is the language to use. Is this a reasonable position given how much faster JavaScript has become?

JavaScript in the browser is getting faster and this has resulted in some claims that it is so fast you don't need anything else. Be it 3D games or complex simulations, there are examples of JavaScript doing an impressive job, but how much faster are competing technologies? How much of the machine are we throwing away by using JavaScript?

Joel Webber has performed some comparisons of JavaScript under various engines, Java, and native code in various environments. The benchmark used is Box2D, the widely used physics engine that is responsible for the way you throw in Angry Birds. The reason for the choice is that it is a complex piece of code that isn't amenable to easy optimization. It also happens to be available in many different forms, having been ported to al the major environments. No attempt was made to make the benchmarks running on different systems in any way comparable, but given the importance of the code it is reasonable to assume that they represent reasonable attempts at creating something efficient. This is a very real world comparison of how the different systems perform.

The benchmark was used to compare performance under native C++, Google Chrome's NaCl native API, Java and a hand translated JavaScript running on a number of browsers. Also JavaScript generated from C++ and Java were tested.

The tests revealed that the systems separated into three groups -  the native code at around 5ms, JVM code at around 10ms and JavaScript at around 100ms.

 

javascriptperformanceSource

While the JavaScript might be impressively fast notice that it is more than 20x slower than the native code. 

Of the native code approaches, NaCl in Chrome17 was slower but comparable to raw C++.  Of the rest, the real surprise was that the spread of results caused by the different browsers wasn't as big as the difference between hand generated and auto generated JavaScript  - with the auto-generated being faster. The fastest performance in the JavaScript group was C++ code complied to JavaScript using Mandreel.

So JavaScript implementations are quite a way from reaching native code performance. Even the highly optimized Java JVM is roughly three time slower than native code.

Perhaps we should not be surprised by the results. However as with all benchmarks this is just an indication of the state of things - only one machine was used and one highly specify non-optimized task. The results will either back up your prejudices or be another piece of evidence you reject as spurious. 

More information

You can see all of the results and a full discussion of the methods used in

Box2D as a measure of runtime performance

 

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter or Facebook or sign up for our weekly newsletter.


 

Banner


Quantum Computers Really Are A One Trick Pony
17/03/2024

Google is offering $5 million if you can think up a use for a quantum computer. Wait, I thought quantum computers were the next big thing, a revolution! Surely we know what they can do?



Generative AI Training For All On Coursera
04/03/2024

Generative AI is on the loose, getting into business and commerce as well as into art, poetry and coding. Already useful, it  will become ever more useful as long as we use it properly. Coursera  [ ... ]


More News

 

<ASIN:0596805527>

<ASIN:0596517742>

Last Updated ( Saturday, 17 December 2011 )