Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 246 posts at DZone. You can read more from them at their website. View Full User Profile

What is the JavaScript Equivalent of Java Bytecode?

01.31.2011
| 8996 views |
  • submit to reddit
The browser is slowly turning into a great platform. Part of the allure of the Java platform is that it has a core that goes beyond “Java the language”: The Java Virtual Machine (JVM). There are now many languages that target the JVM, for example: Groovy, JRuby, Fantom, Jython, Scala, ABCL, Clojure, and Gosu. Java class files store JVM programs as Java bytecode, a compact binary format that source code is compiled to. Does JavaScript have something similar?
Short answer: No. The situation is as follows.
  • Compiling to JavaScript: JavaScript engines have become so fast that it is viable to compile other languages to JS source. That is, we have virtual machines that are fed source code! Thus, for the purpose of storing and exchanging programs, JavaScript source code corresponds to Java bytecode. Parsing JavaScript is fast, so it does not present too much of a problem as an intermediate step. There are also examples of compiling JavaScript (or something very similar) to JavaScript: the Closure Compiler, Qooxdoo, CoffeeScript. Lastly, GWT even compiles a static language (Java) to a dynamic one (JavaScript).
  • Minifying JavaScript source: Minification can be used to reduce the size of source code. The simplest kind of minification is to remove comments. But further compression can be achieved. Take, for example, the following source code [source: N. C. Zakas].
        function sum(num1, num2) {
            return num1 + num2;
        }
    
    This is reduced by YUI Compressor to
        function sum(A,B){return A+B;}
    
    Zakas gives more details.
  • Core JavaScript: Being the interface to JS engines, it helps that JavaScript is a relatively simple language. But what if it were even simpler? There could be a “Core JavaScript”, a subset of JS that all other languages are compiled to. That would include more sophisticated JavaScript language features and future JS versions. As an aside, compiling to a core language is a common technique in the Lisp community.
  • Binary exchange format: I don’t think we could get JavaScript engines to agree on a common binary format, because there is no common ground: Firefox, Safari and Internet Explorer each use different bytecode, Google’s V8 compiles directly to machine code. Thus, it is obvious why there is no common binary format and why JavaScript has no direct analog to Java class files. But Core JavaScript could serve the same purpose. For faster loading, compactness, and possibly obfuscation, the abstract syntax tree could be used (an extreme way of minification, if you will).
To summarize: The motto “source code is the new binary” and not minding the parsing on the web is a surprising development. Source code minification is also interesting, but a logical consequence of this motto.

Related reading:



From http://www.2ality.com/2011/01/what-is-javascript-equivalent-of-java.html

 

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Dominique De Vito replied on Tue, 2011/02/01 - 2:30am

Well, JavaScript is not exactly equivalent to Java Bytecode, but close to.

JavaScript may be better seen, from Java, as an assembly language (because JavaScript  is a language while bytecodes are more hardly seen as a language); while JavaScript is still, of course, more than that from non-Java perspective.

 

In January 207, I wrote: Why not running a Java Browser Edition on top of Firefox VM (Tamarin) ?

So, in October 2009 , I realised: Google has released a (kind of) Java Browser Edition with GWT

On the other side, SUN has paid a great price forgetting the HTML juggernaut and then, not leveraging JWebPane...

 

Axel Rauschmayer replied on Tue, 2011/02/01 - 4:06am in response to: Dominique De Vito

Amen to that. Sun dropped the ball regarding the web. Desktop Java is also much less than it could be.

Stephen Colebourne replied on Wed, 2011/02/02 - 6:50pm

The Fantom language compiles down to Javascript today - http://fantom.org/doc/docLang/JavaScript.html

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.