21

I'm trying to get a better understanding of how JavaScript is executed in a web browser environment.

In terms of Node.js, I understand that the JavaScript code written in a Node.JS program is compiled with C++ code (V8), and ultimately becomes machine code. Since Node.js can interact with the filesystem and other machine level tasks, to me it makes sense why it has to eventually become machine code.

I feel differently about the web browser environment. From my understanding, the main goal is to interact with the DOM. Does JavaScript need to be compiled into machine code to just interact with the DOM?

I'm puzzled by this. Node.js and Chrome both run on V8. V8 is written in C++ and to my knowledge, compiles JavaScript code into machine code a processor can understand.

You need a JavaScript engine to implement ECMA-262, that is the whole purpose of an engine (I think?). But, does a web browser need JavaScript to be compiled to a Machine Language Level, what machine operations is it performing?

Here are a few articles I've researched, unfortunately, I haven't found an answer to my question in them:

Community
  • 1
  • 1
HelloWorld
  • 10,529
  • 10
  • 31
  • 50
  • I don't have a full answer to your question, but this is going to depend on the browser at the very least, and what js engine it uses. Most the big players have their own unique engines or at least their own forks of existing engines. – S. Buda Oct 10 '16 at 22:50
  • 1
    Who knows? Maybe it is, maybe it isn't. Modern JavaScript runtimes may indeed compile small pieces of code into native binary form. There's really no way your program can tell. Why does it matter? – Pointy Oct 10 '16 at 22:51
  • And yes, as Jaromanda X says, you're completely wrong about how Node works. – Pointy Oct 10 '16 at 22:51
  • @JaromandaX my understanding is, Node.js is written in JS and uses V8 to implement JavaScript. The JavaScript code written is interpreted by V8 and compiled into machine code. If this isn't how it works, can you please link me to a resource so I can get a better understanding? – HelloWorld Oct 10 '16 at 22:53
  • The point is that javascript is suposed to be interpreted, it would cost a lot compile and execute a program in those situation. And of course there is lots of feature that could be compiled and delivered as a native part of the engine but it's not the case of browsers in general. – lenilsondc Oct 10 '16 at 22:57
  • @HelloWorld V8 in Node is the same V8 that's in Chrome. It works in pretty much the same way. It would help a lot if you'd explain why this stuff is important to you. – Pointy Oct 10 '16 at 22:57
  • @LenilsondeCastro modern JavaScript runtimes perform all sorts of complicated and expensive optimizations, and there's no reason to expect that transforming basic code blocks into native machine code isn't included. It's true however that the word "compiled" is probably not applicable, as it's more like "just in time" optimization. – Pointy Oct 10 '16 at 22:59
  • @Pointy you're right and also it doesn't count as compile activity. Also, I think the difference between a compiler and an interpretor that's causing the missunderstanding here. – lenilsondc Oct 10 '16 at 23:02
  • @Pointy, I am just trying to build a better understanding of what a javascript engine does and how the code eventually gets to the microprocessor level. – HelloWorld Oct 10 '16 at 23:03
  • Also, quoting Kyle Simpson "JavaScript falls under the general category of "dynamic" or "interpreted" languages, it is in fact a compiled language. It is not compiled well in advance, as are many traditionally-compiled languages, nor are the results of compilation portable among various distributed systems." [You Don't Know JS](https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch1.md#compiler-theory). Everyone keeps quoting that JavaScript is an interpreted language here, is it really based on what Kyle states? – HelloWorld Oct 10 '16 at 23:07
  • 2
    The second link in the question contains the following: *It compiles JavaScript code into machine code at execution by implementing a **JIT (Just-In-Time) compiler** ... The main difference with V8 is that it doesn’t produce bytecode or any intermediate code.* – Barmar Oct 10 '16 at 23:08
  • Having read [this](http://creativejs.com/2013/06/the-race-for-speed-part-2-how-javascript-compilers-work/) - JIT means that the engine **does** convert code to native machine code!!! I apologise for my previous misdirection! – Jaromanda X Oct 10 '16 at 23:09
  • `Everyone keeps quoting that JavaScript is an interpreted language here` - it actually **is** interpreted ... and compiled "just in time". If it were only compiled, the javascript would need to be compiled for every known (and unknown) CPU+OS combination BEFORE being deployed to a web page - clearly that's not what anyone does! The engine interprets and compiles as required on the fly for the architecture it is running on – Jaromanda X Oct 10 '16 at 23:14
  • @JaromandaX, you are not wrong. Also, it wouldn't change the fact tha javascript is an interpreted language, wouldn't it? Even thoung V8 eventualy "compiles" the code, still is an interpreted language. All JIT's features in my opinion is part of the optmization to use right resources from the envirorment that VM is working on. – lenilsondc Oct 10 '16 at 23:17
  • I originally said (comments removed) that V8 does not "compile JS to machine code" - this was the misdirection I was referring to – Jaromanda X Oct 10 '16 at 23:23
  • So the answer has been given. Yes, javascript is compilied (Just-in-Time) to machine code by engines, v8 at least. But is that general? what about other engines? – lenilsondc Oct 10 '16 at 23:35
  • 1
    @LenilsondeCastro nitro, spidermonkey, chakra and v8 do. – Darkrum Oct 10 '16 at 23:58
  • @HelloWorld the definition of the language is implicitly intended to make the way that it's actually implemented on any particular computing hardware *completely irrelevant*. What difference does it make when you're making a "click" handler for a button how the code is actually executed, so long as it behaves according to the spec? – Pointy Oct 11 '16 at 01:25
  • @Pointy, I understand what you are saying, it doesn't make a difference. For my own general knowledge and curiosity, I would like to know how JS works under the hood. It started by reading through the V8 source code, I've been learning some C++ in an online course I've been taking because I would like to understand how javascript works in different environments under the hood. Learning a little C++ has helped me gain at the least, some sort of understanding of the V8 engine, even though I would like to have a better understanding which is why I posted the question initially. – HelloWorld Oct 11 '16 at 14:51

1 Answers1

9

The engine is written in C++, then this code is translated into machine code by a compiler. Once the code is in machine language, it can be run by the computer. While the engine is running, it can read code written in JavaScript, interpret it, and execute what the code is asking it to do. In this case, what is actually running in the computer is the engine code, that just happens to be doing what another code is telling it to do. The difference between node and a browser is that the browser won't do anything that a JavaScript is asking it to do. Another thing to keep in mind is that some browsers and node translates JavaScript code to machine code in real time to get more speed. Browsers are also careful not to write machine code that is dangerous, but in theory that could happen.

Rodrigo5244
  • 5,145
  • 2
  • 25
  • 36