admin管理员组文章数量:1401149
So I am asking does each web browser have there own piler example IE piles Javascript from a website and generates sequence A of byte code .
On the other hand, google chrome piles 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 piler 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.
So I am asking does each web browser have there own piler example IE piles Javascript from a website and generates sequence A of byte code .
On the other hand, google chrome piles 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 piler 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.
Share Improve this question edited Aug 13, 2017 at 1:10 Talha Awan 4,6194 gold badges26 silver badges40 bronze badges asked May 1, 2015 at 17:22 nadernader 1412 silver badges14 bronze badges 6- 3 Browsers don't pile JavaScript; they interpret it. – ProgramFOX Commented May 1, 2015 at 17:24
- 5 @ProgramFOX in the old days this was true, nowadays they also pile to byte code – torox Commented May 1, 2015 at 17:25
- There are different script engines, yes: en.wikipedia/wiki/List_of_ECMAScript_engines But I'm not sure what you're proposing as a solution would be an option. – David Commented May 1, 2015 at 17:28
- @David thanks . I was wondering about that . I know it could be beneficial in some scenarios . So I am just trying to get more educated on web browser and website code interactions . Thanks torox too. – nader Commented May 1, 2015 at 17:30
- 1 the pilation step takes but a millisecond or two anyway (parsing takes longer) so you wouldn't be saving much, and you would potentially have a lot more code to ship to the client. stick with sharing source. – dandavis Commented May 1, 2015 at 18:36
4 Answers
Reset to default 5As others have pointed out, there are different ECMAScript engines and some of them use a JIT (Just-In-Time) piler 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./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 piled or prepiled 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 piler 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:
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/blog/asmjs-javascript-pile-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/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 remend that you take a look at the FAQ first, and then you could start playing with it in the online tool.
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 patibility table for ES5 (the one you're writing in today): http://kangax.github.io/pat-table/es5/
And here's same thing for shiny-new ES6: http://kangax.github.io/pat-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 piled or interpreted language: it is definitely interpreted language - at least originally. But most mon JavaScript engines in use today implement JIT (Just-In-Time piler), translating much of JavaScript to byte or machine code before executing it (ergo - piling).
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?
Yes, each browser has its own implementation of an ECMAScript engine, most monly implementing/supporting ECMA-262, monly known as JavaScript. While there are several large related families of browser engines such as Webkit, each engine further can have its own JavaScript engine. For example, as many have pointed out, Google use the V8 engine. Because these engines each do things a little differently, there is no one set of code that is promised to be deterministic across them, the way say Java code will run the same on any machine that supports the JVM.
Inherently, JavaScript is not piled like a traditional language such a Java or C/C++. This is why, without the help of a 3rd party program, you cannot find non-syntax errors in your JavaScript code until that code runs. ECMAScript is an interpreted language.
Now, this is the tricky part. Most modern JavaScript engines do in fact pile JavaScript, often to another language (also known as Source-to-Source piling or transpiling) such as C, to perform performance optimizations on it. Of course, at some point, all code gets piled into byte code.
Your best bet for writing JavaScript that will work on all major browsers is to use core/standard features. For example, this means passing timestamp string in the form of "yyyy/mm/dd" instead of "yyy-mm-dd" when using new Date()
since Firefox does not support the latter format - the Chrome developers simply added it to be nice to you. IE is notorious for handling certain non-standard features differently. I'm a big fan of http://caniuse./ to help with this.
Nowadays most javascript engines are JIT pilers. More here: What does a just-in-time (JIT) piler do?
So yes, javascript is piled (not interpreted), and most major browsers do it differently.
本文标签: Do compilers for javascript differ from web browser to web browserStack Overflow
版权声明:本文标题:Do compilers for javascript differ from web browser to web browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744215457a2595618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论