javascript编译_JavaScript:解释还是编译?

javascript编译

Computers cannot actually run the code that you write in JavaScript (or any other language for that matter). Computers can only run machine code. The machine code that a particular computer can run is defined within the processor that is going to run those commands and can be different for different processors.

计算机实际上无法运行您用JavaScript (或与此相关的任何其他语言)编写的代码。 计算机只能运行机器代码。 特定计算机可以运行的机器代码在将要运行这些命令的处理器中定义,并且对于不同的处理器可以不同。

Obviously, writing machine code was difficult for people to do (is 125 an add command or is it 126 or perhaps 27). To get around that problem what are known as assembly languages were created. These languages used more obvious names for the commands (such as ADD for adding) and thus did away with the need to remember the exact machine codes. Assembly languages still have a one to one relationship with the particular processor and machine code that the computer converts those commands into.

显然,人们很难编写机器代码 (添加命令是125还是126或27)。 为了解决该问题,创建了所谓的汇编语言。 这些语言为命令使用了更明显的名称(例如,用于添加的ADD),因此无需记住确切的机器代码。 汇编语言仍然与计算机将这些命令转换成的特定处理器和机器代码一一对应。

汇编语言必须经过编译或解释 ( Assembly Languages Must Be Compiled or Interpreted )

Very early on it was realized that easier to write languages were needed and that the computer itself could be used to translate those into the machine code instructions that the computer can actually understand. There were two approaches that could be taken with this translation and both alternatives were chosen (either one or the other will be used depending on the language being used and where it is being run).

在很早的时候就意识到需要更容易编写语言 ,并且计算机本身可以用来将其翻译成计算机可以实际理解的机器代码指令。 此翻译可以采用两种方法,并且选择了两种选择(根据使用的语言和运行的位置,选择使用哪种方法)。

A compiled language is one where once the program has been written you feed the code through a program called a compiler and that produces a machine code version of the program. When you want to then run the program you just call the machine code version. If you make changes to the program you need to recompile it before being able to test the changed code.

编译语言是一种语言,一旦编写了程序,便会通过称为编译器的程序来提供代码,并生成该程序的机器代码版本。 如果要运行该程序,则只需调用机器代码版本。 如果对程序进行了更改,则需要先重新编译该程序,然后才能测试更改后的代码。

An interpreted language is one where the instructions are converted from what you have written into machine code as the program is being run. An interpreted language basically gets an instruction from the program source, converts it to machine code, runs that machine code and then grabs the next instruction from the source to repeat the process.

解释语言是一种在程序运行时将指令从您编写的指令转换为机器代码的语言。 解释型语言基本上从程序源获取指令,将其转换为机器代码,运行该机器代码,然后从源中获取下一条指令以重复该过程。

编译和解释的两个变体 ( Two Variants on Compiling and Interpreting )

One variant uses a two-stage process. With this variant, the source of your program is compiled not directly into the machine code but instead is converted to an assembly-like language that is still independent of the particular processor. When you want to run the code it then processes that compiled code through an interpreter specific to the processor so as to get the machine code appropriate to that processor. This approach has many of the benefits of compiling while maintaining processor independence since the same compiled code can be interpreted by many different processors. Java is one language that often uses this variant.

一种变体使用两阶段过程。 使用此变体,您的程序源代码不会直接编译为机器代码,而是会转换为仍独立于特定处理器的类似于汇编的语言。 当您要运行代码时,它将通过特定于处理器的解释器来处理已编译的代码,以获取适合该处理器的机器代码。 这种方法在保持处理器独立性的同时具有编译的许多好处,因为相同的编译代码可以由许多不同的处理器解释。 Java是经常使用此变体的一种语言。

The other variant is called a Just in Time compiler (or JIT). With this approach, you don't actually run the compiler after you have written your code. Instead, that happens automatically when you run the code. Using a Just in Time compiler the code isn't interpreted statement by statement, it is compiled all in one go each time when it is called to be run and then the compiled version that it just created is what gets run. This approach makes it look a lot like the code is being interpreted except that instead of errors only being found when the statement with the error is reached, any errors detected by the compiler result in none of the code being run instead of all of the code up to that point being run. PHP is an example of a language that usually uses just in time compilation.

另一个变种称为即时编译器(JIT)。 使用这种方法,您在编写代码后实际上不会运行编译器。 而是在运行代码时自动发生。 使用即时编译器时,不会逐条解释代码,每次调用运行时都会一次编译所有代码,然后运行刚刚创建的编译版本。 这种方法看起来很像代码正在被解释,除了不是仅在到达带有错误的语句时才发现错误,编译器检测到的任何错误都导致没有代码运行而不是所有代码到那时为止。 PHP是通常仅在及时编译中使用的一种语言示例。

JavaScript是否已编译或解释? ( Is JavaScript Compiled or Interpreted? )

So now we know what interpreted code and compiled code mean, the question we next need to answer is what does all of this have to do with JavaScript? Depending on exactly where you run your JavaScript the code may be compiled or interpreted or use either of the other two variants mentioned. Most of the time you are ​running your JavaScript in a web browser and there the JavaScript is usually interpreted.

因此,现在我们知道了解释代码和编译代码的含义,我们接下来需要回答的问题是所有这些与JavaScript有什么关系? 根据运行JavaScript的确切位置,可以编译或解释代码,也可以使用上面提到的其他两个变体中的任何一个。 大多数时候,你运行你JavaScript在Web浏览器 ,并在那里JavaScript通常被理解。

Interpreted languages are usually slower than compiled languages. There are two reasons for this. Firstly the code to be interpreted actually has to be interpreted before it can be run and secondly, that has to happen every time that the statement is to be run (not only every time you run the JavaScript but if it is in a loop then it needs to be done every time around the loop). This means that code written in JavaScript will run slower than code written in many other languages.

口译语言通常比编译语言要慢。 有两个原因。 首先,实际上必须先解释要解释的代码,然后才能运行它;其次,它必须在每次要运行该语句时发生(不仅是每次运行JavaScript时,而且如果它处于循环中,那么它都必须运行)每次循环都需要完成)。 这意味着用JavaScript编写的代码将比用许多其他语言编写的代码运行慢。

How does knowing this help us where JavaScript is the only language available for us to run across all web browsers? The JavaScript interpreter itself that is built into the web browser is not written in JavaScript. Instead, it is written in some other language that was then compiled. What this means is that you can make your JavaScript run faster if you can take advantage of any commands that JavaScript provides that allow you to offload the task to the JavaScript engine itself.

知道这一点如何帮助我们在JavaScript是唯一可在所有Web浏览器上运行的语言? Web浏览器中内置JavaScript解释器本身不是用JavaScript编写的。 相反,它是用其他某种语言编写的,然后再进行编译。 这意味着如果可以利用JavaScript提供的任何命令来使任务卸载到JavaScript引擎本身,则可以提高JavaScript的运行速度。

使JavaScript更快运行的示例 ( Examples for Getting JavaScript to Run Faster )

An example of this is that some but not all browsers have implemented a document.getElementsByClassName() method within the JavaScript engine while others have yet to do so. When we need this particular functionality we can make out code run faster in those browsers where the JavaScript engine provides it by using feature sensing to see if the method already exists and only creating our own version of that code in JavaScript when the JavaScript engine doesn't provide it for us. Where the JavaScript engine does provide that functionality it should run faster if we use that rather than running our own version written in JavaScript. The same applies to any processing that the JavaScript engine makes available for us to call directly.

这样的一个例子是,某些(但不是全部)浏览器已经在JavaScript引擎中实现了document.getElementsByClassName()方法,而其他浏览器尚未实现。 当我们需要此特定功能时,可以通过使用功能感应来查看方法是否已经存在,并仅当JavaScript引擎不存在时才在JavaScript中创建我们自己的代码版本,才能使代码在JavaScript引擎提供的浏览器中更快地运行。为我们提供它。 在JavaScript引擎确实提供该功能的地方,如果我们使用它而不是运行我们自己的用JavaScript编写的版本,它应该运行得更快。 这同样适用于JavaScript引擎可用于我们直接调用的任何处理。

There will also be instances where JavaScript provides multiple ways of making the same request. In those instances, one of the ways of accessing the information may be more specific than the other. For example document.getElementsByTagName('table')[0].tBodies and document.getElementsByTagName('table')[0].getElementsByTagName('tbody') both retrieve the same nodelist of the tbody tags in the first table in the web page however the first of these is a specific command for retrieving the tbody tags where the second identifies that we are retrieving tbody tags in a parameter and other values can be substituted to retrieve other tags. In most browsers, the shorter and more specific variant of the code will run faster (in some instances much faster) than the second variant and so it makes sense to use the shorter and more specific version. It also makes the code easier to read and maintain.

在某些情况下,JavaScript提供了多种方法来发出相同的请求。 在那些情况下,一种访问信息的方式可能比另一种更为具体。 例如document.getElementsByTagName('table')[0] .tBodies和document.getElementsByTagName('table')[0] .getElementsByTagName('tbody')都检索网络中第一个表中tbody标签的相同节点列表页面,但是其中的第一个是用于检索tbody标签的特定命令,其中第二个命令标识我们正在参数中检索tbody标签,并且可以替换其他值来检索其他标签。 在大多数浏览器中,较短和更具体的代码变体比第二个变体运行得更快(在某些情况下要快得多),因此使用较短和更具体的版本是有意义的。 这也使代码更易于阅读和维护。

Now in many of these cases, the actual difference in the processing time will be very small and it will only be when you add many such code choices together that you will get any noticeable difference in the time your code takes to run. It is fairly rare though that changing your code to make it run faster is going to make the code significantly longer or harder to maintain, and often the reverse will be true.There is also the added benefit that future versions of JavaScript engines may be created that speed up the more specific variant even further so that using the specific variant may mean that your code will run faster in the future without you having to change anything.

现在,在许多情况下,处理时间的实际差异将非常小,只有将许多这样的代码选择加在一起时,您的代码运行时间才会有任何明显的差异。 尽管更改代码以使其运行得更快将使代码变得更长或更难维护,这是相当罕见的,但通常情况恰恰相反。此外,还可以创建将来版本JavaScript引擎,这还有一个额外的好处。可以进一步加快更具体的变体的速度,因此使用特定变体可能意味着您的代码将来可以更快地运行而无需更改任何内容。

翻译自: https://www.thoughtco.com/interpreted-or-compiled-2037502

javascript编译

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值