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.



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.



Dart Adds WebAssembly Support

Google has released Dart 3.3 with experimental support for applications compiled to WebAssembly, along with new extension types and a revamped JavaScript interop model.

Apple - Master Of Malicious Compliance

Apple is under pressure to open up its closed eco system, but it still has the power to resist. It is difficult to force a horse to drink and it is turning out very difficult to make Apple comply with [ ... ]

More News




Last Updated ( Saturday, 17 December 2011 )