Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do compilers for javascript differ from web browser to web browser

So I am asking does each web browser have there own compiler example IE compiles Javascript from a website and generates sequence A of byte code .

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

I am wondering this because if that is the case would it be beneficial to run the compiler on the Javascript and upload the generated byte code to the website rather than the Javascript itself. And send different byte code based on each browser.

Or are there some other limitations.

like image 725
nader Avatar asked May 01 '15 17:05

nader


People also ask

Does JavaScript work the same on every browser?

The Anatomy of the JavaScript engine. EcmaScript specification tells how JavaScript should be implemented by the browser so that a JavaScript program runs exactly the same in all the browsers, but it does not tell how JavaScript should run inside these browsers. It is up to the browser vendor to decide.

Is browser a compiler for JavaScript?

In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.

What compiler should I use for JavaScript?

Visual Studio Code It is the most popular IDE in the market not only for JS but several other languages since it supports more than 40 languages. It is a free and cross-platform IDE that works very well for the development of frontend items.

What makes JavaScript same in all browsers?

All major web browsers have a dedicated JavaScript engine to execute the code on users' devices. JavaScript is a high-level, often just-in-time compiled language that conforms to the ECMAScript standard. It has dynamic typing, prototype-based object-orientation, and first-class functions.


2 Answers

As others have pointed out, there are different ECMAScript engines and some of them use a JIT (Just-In-Time) compiler while some others use runtime interpreters, being the former the preferred option for most browsers nowadays as it gives some performance benefits over the latter option.

You can see another question about this on: 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 into C++ applications.

About your idea of sending compiled or precompiled code to the client instead of the 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 (such as C) is translated by a source-to-source compiler such as Emscripten (based on LLVM). Performance is improved by limiting language features to those amenable to ahead-of-time optimization and other performance improvements.

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

Asm.js Compilation and Execution Pipeline

Of course, there are also a lot of restrictions about what you can do with it, as it is mainly oriented to work with just numbers. See http://ejohn.org/blog/asmjs-javascript-compile-target/

Real support for Asm.js is still a limitation, so you can't use things like "use asm" and although you can run Asm.js code on today browsers and get some performance improvements, it won't be as good as it could be in browsers that could optimize Asm.js code. However, we may start having that and some other improvements in the (hope that near) 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 purpose JS that needs to work with more than just numbers, you can use Google Closure Compiler. I would recommend that you take a look at the FAQ first, and then you could start playing with it in the online tool.

like image 135
Danziger Avatar answered Oct 02 '22 01:10

Danziger


There are several JavaScript (or rather ECMAScript) implementations in wide use, and while in theory there are standards, most widely used one being ES5 (ECMAScript 5) - yes, not everything in all browsers is properly, consistently implemented (I'm looking at you, old IE).

Here's nice compatibility table for ES5 (the one you're writing in today): http://kangax.github.io/compat-table/es5/

And here's same thing for shiny-new ES6: http://kangax.github.io/compat-table/es6/

Note the disclaimer at the top of these tables:

Please note that some of these tests represent existence, not functionality or full conformance.

Also, on the issue of whether JavaScript is compiled or interpreted language: it is definitely interpreted language - at least originally. But most common JavaScript engines in use today implement JIT (Just-In-Time compiler), translating much of JavaScript to byte or machine code before executing it (ergo - compiling).

Most widely used (and most performant as well) of these engines is V8, used by WebKit (and therefore present in Chrome, Safari, Opera, ... - Node.JS is using it as well). Read more about V8 and its JIT implementation: How the V8 engine works?

like image 41
bardzusny Avatar answered Oct 02 '22 01:10

bardzusny