简体   繁体   English

为什么 JavaScript 被编译为机器码?

[英]Why does JavaScript get compiled to machine code?

I recently started some web development, with ASP.NET and some Javascript, and something is confusing me alot.我最近开始了一些 web 开发,包括 ASP.NET 和一些 Javascript,有些事情让我很困惑。

I always read that JavaScript used to be interpreted until JIT slowly made it so chunks are compiled to machine code (which made browsers alot faster).我总是读到 JavaScript 曾经被解释,直到 JIT 慢慢地把它编译成机器代码(这使得浏览器更快)。

This makes no sense to me.这对我来说毫无意义。 How can JavaScript compile to native machine code, if traditional JavaScript apps don't target the machine/CPU to begin with?如果传统的 JavaScript 应用程序不以机器/CPU 为目标,那么 JavaScript 如何编译为本机机器代码?

I understand if an electron.js app gets compiled to machine code using the NodeJS runtime.我了解 electron.js 应用程序是否使用 NodeJS 运行时编译为机器代码。 That I get.我明白了。 Because it natively compiles to machine code and as far as I understand it, doesn't run in a browser .因为它本机编译为机器代码,据我所知,它不能在浏览器中运行

If traditional JavaScript apps run in a browser, why must it be compiled to machine code?如果传统的 JavaScript 应用程序在浏览器中运行,为什么必须将其编译为机器码? The browser is responsible for running the code, not the CPU.浏览器负责运行代码,而不是 CPU。 The CPU runs the browser itself. CPU 运行浏览器本身。 I actually don't see how the native OS can influence anything that happens in the browser at all or vise versa.我实际上根本看不到本机操作系统如何影响浏览器中发生的任何事情,反之亦然。 Seems like a security issue as well.似乎也是一个安全问题。

Sorry if it's a stupid question, but I can't find any resource that will go beyond saying "Javascript uses JIT"抱歉,如果这是一个愚蠢的问题,但除了说“Javascript 使用 JIT”之外,我找不到任何可以 go 的资源

Thank you!谢谢!

Lauren劳伦

At the end of the day, the CPU has to run the code.归根结底,CPU 必须运行代码。

JIT-compiling it down to machine code is one way to make that faster. JIT 将其编译为机器代码是加快速度的一种方法。

How can JavaScript compile to native machine code, if traditional JavaScript apps don't target the machine/CPU to begin with?如果传统的 JavaScript 应用程序不以机器/CPU 为目标,那么 JavaScript 如何编译为本机机器代码?

It is not "Javascript" that is doing it, it is the browser (or rather, the Javascript execution engine inside the browser), and since it is "JIT" it knowns exactly which CPU to target (this is not done in a generic way, this is done for the specific CPU that the browser is currently running on).执行此操作的不是“Javascript”,而是浏览器(或者更确切地说,浏览器内的 Javascript 执行引擎),并且由于它是“JIT”,它确切地知道要定位哪个 CPU(这不是在通用中完成的方式,这是针对浏览器当前运行的特定 CPU 完成的)。

So, yes, there is some mismatch, since Javascript will not use low-level primitive types that the CPU can work with directly, which is why there is a lot of indirection and speculative type inference guess-work.所以,是的,存在一些不匹配,因为 Javascript 不会使用 CPU 可以直接使用的低级原始类型,这就是为什么有很多间接和推测类型推断猜测工作的原因。 The resulting machine code is much different than you would get from hand-coded assembly, but it can still be a net positive.生成的机器代码与您从手动编码的汇编中获得的代码有很大不同,但它仍然可以是净正面的。 To help with this, WASM was developed, which is closer to "normal" machine code.为了解决这个问题,开发了 WASM,它更接近“正常”机器代码。

Other intermediate, non-CPU specific formats like JVM bytecode or CLR bytecode or LLVM bitcode are in a similar situation (in that can also be compiled to machine code they do not themselves target directly) -- but they have been "lowered" already from language source code to something close to machine code.其他中间的、非 CPU 特定的格式,如 JVM 字节码或 CLR 字节码或 LLVM 位码处于类似的情况(也可以编译为它们本身并不直接针对的机器代码)——但它们已经从语言源代码接近机器代码。

Seems like a security issue as well.似乎也是一个安全问题。

Yes, it can be.是的,可以。 The browser has to be careful in what it is doing here, and the OS should sandbox the browser as much as possible.浏览器在这里所做的事情必须小心,并且操作系统应该尽可能地对浏览器进行沙箱处理。

Executing instructions is easier than running an interpreter, and JIT seeks to take advantage of this for a performance boost.执行指令比运行解释器更容易,JIT 试图利用这一点来提高性能。 All programs running on your computer become machine code at some point, the only question is which instructions are be executed.在您的计算机上运行的所有程序在某些时候都会变成机器代码,唯一的问题是执行哪些指令。

let x=0;
for (let i=0;i<100;++i) {
  x+=2;
}

Since it is clear that there are no side effects in a block of code like this, it is faster to compile instructions directly, rather than interpreting each line.由于很明显这样的代码块没有副作用,因此直接编译指令比解释每一行代码更快。

// NIOS 2 assembly, sorry its the only one i know
movi r2,0
movi r3,0
movi r4,100
loop:
  addi r2,2
  addi r3,1
  blt r3,r4,loop

Executing this will be faster than executing the parsing logic for each individual instruction.执行此操作将比为每个单独的指令执行解析逻辑更快。

TLDR: All programs are always running CPU instructions, so it is faster to minimize the number of instructions by skipping the parsing stage when possible TLDR:所有程序始终运行 CPU 指令,因此通过尽可能跳过解析阶段来最小化指令数量会更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM