Re: Java 200 times faster, almost as easy to use, so why Javascript ?

Just a quick comment and examples. Hopefully to illustrate some best practices in JS for improved performance.

These JS files you’ve provided make heavy use of global variables, and even then ones which haven’t been scoped with var, The former are slow, and the latter are even slower (and dangerous since they are values shared between instances).

This is true for both the older spidermonkey JS implementation, as well as the new JIT compiled Jaegermonkey implementation. And in the new engine it’s even more important for optimal JIT performance.

I’ve taken a brief moment to revise your examples just comparing your JS with some improved JS files.

Also, some of your examples had codepaths which could possibly be eliminated (e.g. a large loop which sets one variable over and over again, so in effect the only one necessary to set is the final loop value, or if those values are never used in the future, they don’t need to be set at all). So they might not be accurate tests of read/write performance, but rather a test of deadcode elimination. I didn’t have time to investigate whether or not this is the case.

Finally, also when testing you need to execute a function a few times before JS will completely JIT compile, so be sure to execute a few times to get the optimum JIT compiled benchmark.

That said, Java will be faster than JS. No question. I don’t know of any of the new JS implementation which are as fast as the JIT compiled Java VM. Part of this is because of static typing and other language features.

Javascript, however, gains all that nice small talky dynamic typing and binding goodness, and is in many ways more agile than programming in Java, as long as you don’t trip yourself up in the many errors that can happen with dynamic binding when not carefully coded.

Anyway, not trying to get into any language better than another conversation. While I personally mostly code in C, I’m language agnostic. Each language is great for its own reason.