Does JavaScript have a bytecode, like Java?

[2011-01-30] dev, javascript, webdev, jslang
(Ad, please don’t block)
Update 2012-01-22:JavaScript myth: JavaScript needs a standard bytecode” is an updated version of this blog post.

The web and thus JavaScript is slowly turning into a great platform. Part of the allure of the (partially) competing 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). Recently, source maps [3] have allowed one to debug a language that is compiled to JavaScript in its source code.
  • A common virtual machine and/or bytecode: I don’t think we could get browser vendors to agree to a common bytecode for all JavaScript virtual machines, because there is no common ground:
    • JavaScript: Firefox, Safari and Internet Explorer each use different bytecode, Google’s V8 compiles directly to machine code.
    • Non-Javascript languages: Good virtual machines are usually designed specifically for a given programming language. This makes it even harder to find a bytecode that pleases all parties if one includes non-JavaScript languages.
    Thus, it is obvious why there is no common binary format and why JavaScript has no direct analog to Java class files. For faster loading, compactness, and possibly obfuscation, the abstract syntax tree could be used (an extreme way of minification, if you will).
  • 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.
To summarize: The JavaScript motto “source code is the new binary” and not minding the parsing is a surprising development. Source code minification is also interesting, but a logical consequence of this motto.

Related reading:

  1. Proper tail calls in Harmony
  2. Programming languages based on JavaScript VMs
  3. SourceMap on Firefox: source debugging for languages compiled to JavaScript [update: WebKit, too]