7
votes

Is Dart considered to be a compiled or an interpreted language? The same question holds for JavaScript.

The reason for the question:

I've been watching an interview with the founders of dart, and in 7:10 Lars Bak said that:

"When you [...] in a JavaScript program, you actually execute JavaScript before you start running the real program. In Dart, you don't execute anything before the first instruction in main is being executed".

It sounded to me that he's saying that JavaScript is a compiled language while Dart is an interpreted language. Is it true?

Isn't the Dart VM a compiler?

3

3 Answers

14
votes

Depends on the definition of "interpreted" and "compiled" language. And even then, it always depends on the implementation.

What Lars meant is that JavaScript builds its class structures (and other global state) by executing code. In Dart the global state is described by the language syntax and thus only needs parsing (and even then most of it can be skipped first). As a consequence Dart programs can start executing "real" code faster than JavaScript programs.

This obviously only holds for the Dart VM, since programs that have been compiled to JavaScript must use JavaScript mechanisms to build their classes.

Edit (more details):

Take, for example, the following extremely simple class A:

In Dart:

class A {
  final x;
  A(this.x);
  foo(y) => y + x;
}

In JavaScript:

function A(x) { this.x = x; }
A.prototype.foo = function(y) { return y + this.x; }

When the Dart VM starts up it starts by going through the program. It sees the class keyword, reads the class-name (A) and could then simply skip to the end of the class (by counting opening and closing braces, making sure they are not in strings). It does not care for the contents of A, until A is actually instantiated. Now, in reality, it actually looks through the class and finds all the members, but it does not read the contents of methods until they are needed. In any case: it does this in a very fast processing step.

In JavaScript things get more complicated: a fast VM can skip the actual body of the function A (similar to what Dart does), but when it sees A.prototype.foo = ... it needs to execute code to create the prototype object. That is, it needs to allocate a function object (A), look up its prototype property, change this object (adding a new property) with a new function object. In other words: in order to even see that you have a class you need to execute code.

6
votes

Dart as programming language in its primary implementation can be presented as a virtual machine (VM), which is the runtime of programs written in that language.

Current virtual machine implemented as the "just-in-time" (JIT) runtime environment engine.

This means that program not interpreted but compiled. But this compilation process (translating source code to machine instructions) is stretched in time for an unknown period.

This allows virtual machine to defer performing certain operations indefinitely or never performing them.

Assume you have a very big and complex program with a lot of classes which may be never be used in current short lifetime session of program execution.

JIT compilation allow not compile all unused classes but just parse it to special tokens. These tokens later will be used (on demand) for translating them to intermadiate language for constructing machine code.

This process is transparent for user of program. Compiled (to machine code) only that source code that required for the correct working of the program.

Some source code can be never compiled what save a lot of time.

Conclusion:

If Dart language used in its primary state as virtual machine then it compiled to machine code.

-1
votes

Dart compiles into JavaScript, and JavaScript is an interpreted language. Usually, by 'compiled' languages one understands languages which are compiled into platform-specific machine code, run right on the CPU and don't require an interpreter to run, which is not the case for neither JS nor Dart. So I would say that both JS and Dart are interpreted.