interpreter

Interpreter

Computer programs are usually written on high level languages. such as

console.log('Hello');
console.log('World');

The Interpreter is a program the any given high level language and translates each statement of the program at a time into machine code and executes it immediately.

So the output for the above code will be

// Output:
// Hello
// World

Computers don't understand

console.log('Hello');
console.log('World');

However they do understand the a bunch of zeros and ones. i.e 0100100110

In Javascript there are many different available Interpreters to choose from. Here's a list

  • V8 — open source, developed by Google, written in C++

  • Rhino — managed by the Mozilla Foundation, open source, developed entirely in Java

  • SpiderMonkey — the first JavaScript engine, which back in the days powered Netscape Navigator, and today powers Firefox

  • JavaScriptCore — open source, marketed as Nitro and developed by Apple for Safari

  • KJS — KDE’s engine originally developed by Harri Porten for the KDE project’s Konqueror web browser

  • Chakra (JScript9) — Internet Explorer

  • Chakra (JavaScript) — Microsoft Edge

  • Nashorn, open source as part of OpenJDK, written by Oracle Java Languages and Tool Group

  • JerryScript — is a lightweight engine for the Internet of Things.

Here is an example how any Javascript code gets transformed to bytecode and executed

alt text
alt text

Bytecode

Bytecode is an abstraction of machine code.

Here is an example from running node --print-bytecode code.js there is more to it but you can see that its nothing like Javascript and not human readable.

Constant pool (size = 4)
0x1b6759547bd9: [FixedArray] in OldSpace
 - map: 0x1b67cf800729 <Map>
 - length: 4
           0: 0x1b6762dd3cf1 <String[#7]: console>
           1: 0x1b6762dcc421 <String[#3]: log>
           2: 0x1b67595479e1 <String[#5]: Hello>
           3: 0x1b67595479f9 <String[#5]: World>
Handler Table (size = 0)
Source Position Table (size = 16)
0x1b6759547c69 <ByteArray[16]>
[generated bytecode for function: makeRequireFunction (0x1b676791ff89 <SharedFunctionInfo makeRequireFunction>)]
Parameter count 3
Register count 6
Frame size 48

The above bytecode is then executed.

Compiled Languages vs Interpreted Languages

Interpreted Languages

Interpreters run through a program line by line and execute each command.

Examples of common interpreted languages are PHP, Ruby, Python, and JavaScript.

Advantages

  • Easier to implement (writing good compilers is very hard!!)

  • No need to run a compilation stage: can execute code directly "on the fly"

Compiled Languages

Compiled languages is when high level programming languages such as console.log('Hello') are converted directly into machine code that the processor can execute. As a result, they tend to be faster and more efficient to execute than interpreted languages. They also give the developer more control over hardware aspects, like memory management and CPU usage.

Examples of pure compiled languages are C, C++, Erlang, Haskell, Rust, and Go

Advantages

  • Faster performance by directly using the native code of the target machine

  • Opportunity to apply quite powerful optimisations during the compile stage

  • Errors are caught during development

Last updated

Was this helpful?