Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How fast is Javascript compared to Java? [closed]

Are there any tests that compare Javascript's performance with Java's?

UPDATE: Since everyone is asking why the hell this question, here is some context :)

As you all know - I hope - Javascript nowadays doesn't only reside in the web client but also in the web server with node.js.

It could also be run in mobile phones and dekstops with appcelerator and phonegap.

It could also be used substantially in the web browser to make the user experience first class like with desktop applications.

But Java could do these things too, running applets on the web client, and on mobile phones. It's also a language for the backend with many frameworks to choose between.

Since each one of them could almost/entirely replace each other in the mentioned area, I want to know the performance difference between them, for every case I described:

  • Client: Java Applets vs Javascript
  • Server: Java EE vs Javascript with Node.js + Express
  • Mobile phones: Java ME vs Javascript with Phonegap / Appcelerator
  • Desktop: Java SE vs Javascript with Phonegap / Appcelerator

I hope the context is more clear now.

like image 678
never_had_a_name Avatar asked Sep 16 '10 02:09

never_had_a_name


People also ask

How fast is JavaScript compared to Java?

JavaScript is relatively faster than Java because interpreters execute the source program code themselves. JavaScript supports features such as dynamic typing and smaller executable program size. Unlike Java, the JavaScript language can be used in a huge variety of applications.

Why js is slower than Java?

js has a lot of time to do all the computations and transformations for the requests using a single thread. Whereas Java uses one thread for one request, and hence it takes much time to process the concurrent request. Hence the performance of Node. js is high compared to Java performance.

How close is JavaScript to Java?

JavaScript(JS) is not similar or related to Java. Both the languages have a C-like syntax and are widely used in client-side and server-side Web applications, but there are few similarities only. Features of Javascript are as follows: JavaScript was created in the first place for DOM manipulation.


1 Answers

Java and JavaScript are both programming languages. Programming languages are just a bunch of abstract mathematical rules. Programming languages aren't fast. Or slow. They just are.

The performance of an application has nothing to do with the language. The most important factor is the application architecture. Then comes algorithmic efficiency. Then micro-optimizations. Then comes the quality of the compiler/interpreter. Then the CPU. Maybe a couple of other steps in between. The language, however, doesn't directly play a role. (And of course if you're talking about benchmarks, then also the particular benchmark plays a role, as well as how well implemented the benchmark is, how well run it is, whether the guy who performs the benchmark actually knows something about benchmarking, and even more importantly statistics. Also, the precise definition of what you actually mean by "fast" is pretty important, since it can also have significant influence on the benchmark.)

However, the language might indirectly play a role: it is much easier to find and fix performance bottlenecks in 10 lines of highly expressive, clear, concise, readable, well-factored, isolated, high-level Lisp code, than in 100 lines of tangled, low-level C. (Note that those two languages are only examples. I don't mean to single any one language out.) Twitter, for example, have said that with a less expressive language than Ruby, they wouldn't have been able to make such radical changes to their architecture in such a short amount of time, to fix their scalability problems. And the reason why Node.js is able to provide such good evented I/O performance is because JavaScript's standard library is so crappy. (That way, Node.js has to provide all I/O itself, so they can optimize it for evented I/O from the ground up. Ruby and Python, for example, have evented I/O libraries that work just as well as Node.js and are much more mature ... but, Ruby and Python already have large standard libraries, including I/O libraries, all of which are synchronous and don't play well with evented libraries. JavaScript doesn't have the problem of I/O libraries that don't play well with evented I/O, because JavaScript doesn't have I/O libraries at all.)

But if you really want to compare the two, here's an interesting datapoint for you: HotSpot, which is one of the more popular, and also more performant JVM implementations out there, was created by a team of guys which included, among other people, a guy named Lars Bak. But actually, HotSpot didn't appear out of thin air, it was based on the sourcecode of the Anamorphic Smalltalk VM, which was created by a team of guys which included, among other people, a guy named Lars Bak.

V8, which is one of the more popular, and also more performant JavaScript implementations out there, was created by a team of guys which included, among other people, a guy named Lars Bak. But actually, V8 didn't appear out of thin air, it was based on the sourcecode of the Anamorphic Smalltalk VM, which was created by a team of guys which included, among other people, a guy named Lars Bak.

Given that the two are more or less the same, we can expect similar performance. The only difference is that HotSpot has over a hundred engineers working on it for 15 years, whereas V8 has a dozen engineers working for less than 5 years. That is the only difference in performance. It's not about static vs. dynamic typing (Java is statically typed, but most JVMs and certainly HotSpot make no static optimizations whatsoever, all optimizations are purely dynamic), compilation vs. interpretation (HotSpot is actually interpreted with an additional JIT compiler, whereas V8 is purely compiled), high-level vs. low-level. It is purely about money.

But I am going to bet that for every pair of Java and JavaScript implementations where the Java implementation is faster, I can find another pair where the JavaScript implementation is faster. Also, I can probably keep the pair and just use a different benchmark. There's a reason the call the Computer Languages Benchmark Game a "game": they even encourage you right on their own page to play around with the benchmarks to make any arbitrary language rise to the top.

like image 81
Jörg W Mittag Avatar answered Sep 21 '22 23:09

Jörg W Mittag