What is the JavaScript Equivalent of Java Bytecode?

DZone 's Guide to

What is the JavaScript Equivalent of Java Bytecode?

· Java Zone ·
Free Resource
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



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}