Can a compiler be written that took javascript and compiled it into a fluent program, making it more efficient?
I know of just-in-time compilation, but what I'm talking about is taking the javascript and compiling it similar to the way an OS application is compiled.
Just a question for an experiment.
This question has been previously discussed here and here (and presumably in many other places).
The gist of it is this:
JavaScript's language specification does not claim for it to be an interpreted language. It merely defines the syntax and semantics of the language.
Most interpreted languages are first parsed and translated into an intermediary form a.k.a. Byte Code. This process is commonly referred to as "compiling" and that's the job of a compiler *)
Some interpreters of JavaScript are actually compilers / byte code executors, e.g. Google's V8 ("V8 compiles and executes JavaScript source code,..."). Microsoft's Jurassic for compiling .NET to CLI bytecode on the other hand claims to be just a compiler.
Subsequent execution of the program uses the byte code and makes no reference back to the program's JavaScript source code. Jurasic claims to distribute a .NET CLI assembly.
The answer to your question is yes and no, depending on the specific platform you want to target:
yes, JavaScript code can be compiled into intermediary forms that can then be executed by an engine that knows to process this particular form of byte code.
no, there is no generally accepted byte code nor a generic interpreter (a.k.a. Virtual Machine) of such byte code.
Having said that, the issue of "compiled" v.s. "interpreted" languages often seems very much overstated. It is important to realize that there are only gradual differences on how a "binary" as a result of compilation differs from its corresponding "source code". At the end, source and binary are the same thing in different forms, or to paraphrase: it's symbols all the way down.
*) Wikipedia has this definition of a compiler:
A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).