Compilers for javascript vary from web browser to web browser

So, I ask if each web browser has its own example compiler. IE compiles Javascript from the site and generates a sequence of A byte code.

On the other hand, google chrome compiles the same Javascript from the same website and generates a sequence B.

I am interested in this because if this is the case, it would be useful to run the Javascript compiler and upload the generated bytecode to the site, not Javascript itself. And send different byte codes based on each browser.

Or there are some other limitations.

+5
source share
4 answers

As others have pointed out, there are various ECMAScript mechanisms , and some of them use the JIT (Just-In-Time) compiler , while some others use runtime interpreters , being the first preferred option for most browsers at present, as this gives some performance advantages over the last option.

You can see another question about this: https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design

For example, V8 is the JavaScript engine used in Google Chrome, node.js , and can also be embedded in C ++ applications.

About your idea of ​​sending compiled or precompiled code for the client instead of raw JS, there are some projects working on something similar:

Asm.js consists of a strict subset of JavaScript into which code written in statically typed languages ​​with manual memory management (e.g. C) is converted using a source-source-compiler such as Emscripten (based on LLVM ). Performance is improved by limiting language features to those that are amenable to ahead-of-time optimization and other performance improvements.

An important fact related to Asm.js is that existing JavaScript engines work well with its code style, so you can start using it right away! But the code that it produces is still (a subset ) of JS, which we know but is written in some way, which helps JS machines work faster:

Asm.js Compilation and Execution Pipeline

Of course, there are also many limitations on what you can do with it, since it is mainly focused on working with just numbers . See http://ejohn.org/blog/asmjs-javascript-compile-target/

Actual support for Asm.js is still a limitation, so you cannot use things like "use asm" , and although you can run Asm.js code on today's browsers and get some performance improvements, it will not be as good as it could be in browsers that could optimize Asm.js code . However, we can start with this and some other improvements in the (hopefully close) future. See https://blog.mozilla.org/research/2015/02/23/the-emterpreter-run-code-before-it-can-be-parsed/

Meanwhile, and for a more general JS goal that should work with more than just numbers, you can use the Google Closure Compiler . I would recommend looking at the FAQ first , and then you can start playing the online tool with it .

+5
source

There are several JavaScript implementations (or rather ECMAScript ), and although standards exist in theory, one of the most widely used is ES5 (ECMAScript 5) - yes, not everything in all browsers is correctly, consistently implemented (I look at you, old IE ) .

Here's a nice compatibility table for ES5 (the one you write today): http://kangax.imtqy.com/compat-table/es5/

And here is the same for the brilliant new ES6 : http://kangax.imtqy.com/compat-table/es6/

Note the disclaimer at the top of these tables:

Please note that some of these tests are existence, not functionality, or full compliance .

Also, on the question of whether the JavaScript language has been compiled or interpreted: it is definitely an interpreted language - at least initially. But most JavaScript mechanisms currently in use implement JIT (Just-In-Time compiler), which translates most of JavaScript into byte or machine code before executing it (ergo compilation).

The most widely used (and most effective) of these engines are V8 used by WebKit (and therefore are present in Chrome, Safari, Opera, ... - Node.JS using it as well). More on the V8 and its JIT : How does the V8 engine work? p>

+1
source

Yes, each browser has its own implementation of the ECMAScript engine, most often implementing / supporting ECMA-262, commonly known as JavaScript. Although there are several large related browser families, such as Webkit, each engine can have its own JavaScript engine. For example, as many have pointed out, Google uses a V8 engine. Since these engines do things differently, there is no single set of code that promises to be deterministic with respect to them, since Java code will work on any computer that supports the JVM.

Actually, JavaScript does not compile like a traditional language such as Java or C / C ++. That is why, without the help of a third-party program, you cannot find non-syntax errors in your JavaScript code until this code runs. ECMAScript is an interpreted language.

Now this is the hard part. Most modern JavaScript engines actually compile JavaScript, often into another language (also known as Compiling or converting a source to a source), such as C, to perform performance optimization on it. Of course, at some point, all the code is compiled into byte code.

Your best bet for writing JavaScript that will work in all major browsers is to use the basic / standard features. For example, this means that when using new Date() when using new Date() , the timestamp string is used in the form "yyyy / mm / dd" instead of "yyy-mm-dd", since Firefox does not support the latter format - Chrome developers simply added its in nice to you. IE is known for handling some non-standard functions in different ways. I am a big fan of http://caniuse.com/ to help with this.

+1
source

Currently, most javascript engines are JIT compilers. More details here: What exactly does the compiler do (JIT)?

So yes, javascript is compiled (not interpreted), and most major browsers do it differently.

0
source

All Articles