JavaScript 引擎和 JavaScript 运行时环境有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29027845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:59:23  来源:igfitidea点击:

What is the difference between JavaScript Engine and JavaScript Runtime Environment

javascript

提问by clark

I'm feeling a bit confused, could someone help to describe What is the difference between JavaScript Engine and JavaScript Runtime Environment. BTW, Event Loop was implemented in Engine or Runtime?

我感到有点困惑,有人可以帮助描述 JavaScript 引擎和 JavaScript 运行时环境之间的区别是什么。顺便说一句,事件循环是在引擎还是运行时中实现的?

回答by georg

Unlike C and other compiled languages, Javascript runs in a container - a program that reads your js codes and runs them. This program must do two things

与 C 和其他编译语言不同,Javascript 在容器中运行 - 一个读取 js 代码并运行它们的程序。这个程序必须做两件事

  • parse your code and convert it to runnable commands
  • provide some objects to javascript so that it can interact with the outside world.
  • 解析您的代码并将其转换为可运行的命令
  • 为javascript提供一些对象,以便它可以与外部世界进行交互。

The first part is called Engine and the second is Runtime.

第一部分称为引擎,第二部分称为运行时。

For example, the Chrome Browser and node.js use the same Engine - V8, but their Runtimes are different: in Chrome you have the window, DOM objects etc, while node gives you require, Buffers and processes.

例如,Chrome 浏览器和 node.js 使用相同的引擎 - V8,但它们的运行时不同:在 Chrome 中,您有window、DOM 对象等,而 node 为您提供require、缓冲区和进程。

回答by Luo Jiong Hui

Imagine a robot is playing a music:

想象一个机器人正在播放音乐:

  • The JavaScript code would be the music notes to the robot.
  • TheJavaScript engine would be the robot which can understand the notes and act on it.
  • The JavaScript runtime would be the instruments the robot can use in order to play the music.
  • JavaScript 代码将成为机器人的音符。
  • JavaScript 引擎将是能够理解笔记并对其采取行动的机器人。
  • JavaScript 运行时将是机器人可以用来播放音乐的工具。

Imagine a robot is putting out a fire:

想象一个机器人正在灭火:

  • The JavaScript code would be the instructions to the robot to put out a fire.
  • The JavaScript engine would be the robot which can understand the instructions and act on it.
  • The JavaScript runtime would be the fire truck, and the water gun.
  • JavaScript 代码将是机器人灭火的指令。
  • JavaScript 引擎将是能够理解指令并对其采取行动的机器人。
  • JavaScript 运行时将是消防车和水枪。

回答by J?rg W Mittag

Let's first imagine an Ahead-Of-Time compiled implementation of JavaScript.

让我们首先想象一个 JavaScript 的 Ahead-Of-Time 编译实现。

The compilerwill translate JavaScript code to, for example, native x86 machine code that you can run. However, there are some things in JavaScript which happen at runtime, and thus cannot be statically compiled. Garbage Collection, for example, or reflection. So, in order for the program to run, there need to be some kind of support services at runtime, such as the garbage collector and the reflection system. Also, JavaScript has eval, which means that (in our hypothetical compiled implementation) the compiler itself or some other interpreter needs to be available at runtime.

编译器将转换JavaScript代码,例如,原生x86机器代码可以运行。但是,JavaScript 中有一些事情是在运行时发生的,因此无法静态编译。例如,垃圾收集或反射。所以,为了让程序运行起来,在运行时需要有一些支持服务,比如垃圾收集器和反射系统。此外,JavaScript 有eval,这意味着(在我们假设的编译实现中)编译器本身或其他一些解释器需要在运行时可用。

Let's call these things Runtime Support Services.

让我们称这些为运行时支持服务

A second thing that needs to be available for the program, are objects such as Array, Function, Object, etc. and functions such as forEach. This collection of objects and functions that needs to be available to the program from the get-go, is typically called the Core Libraryor Base Library. For a language like JavaScript, which is designed to be embedded, there are also additional libraries which are assumed to be present, depending on the context. For example, for JavaScript embedded in a browser, we expect the DOM objects and functions and the global windowand documentobjects and so on to exist. These may be considered part of the core library too.

第二件事,需要为可用于该程序,是对象如ArrayFunctionObject等,和功能,例如forEach。从一开始就需要对程序可用的对象和函数的集合通常称为核心库基础库。对于像 JavaScript 这样旨在嵌入的语言,根据上下文,还假定存在其他库。例如,对于嵌入在浏览器中的 JavaScript,我们期望 DOM 对象和函数以及全局windowdocument对象等存在。这些也可以被视为核心库的一部分。

And lastly, let's now forget about our hypothetical compiler and look at an interpreter (or JIT compiler or mixed-mode engine or bytecode VM). Here, the Interpreter(or JIT, or whatever) is the third piece of the puzzle. It's the thing that actuallyexecutes the JavaScript program. (Well, technically, a JIT doesn't execute, it compiles, and then something else executes.)

最后,让我们现在忘记我们假设的编译器,看看解释器(或 JIT 编译器或混合模式引擎或字节码 VM)。在这里,解释器(或 JIT,或其他)是拼图的第三部分。它是实际执行 JavaScript 程序的东西。(好吧,从技术上讲,JIT 不会执行,它会编译,然后执行其他一些操作。)

The terminology is not 100% clear: sometimes, only the Runtime Support Services are referred to as Runtime Environment, sometimes, the Core Library is included as well.

术语不是 100% 清楚:有时,只有运行时支持服务被称为运行时环境,有时,核心库也包括在内。

Execution Enginerefers to either just the interpreter (JIT, VM, …) or the combination of interpreter and Runtime Environment. A compiler is never called an Execution Engine (it doesn't execute anything, just translate to another language), and the term Execution Engine is rarely used to refer to a statically compiled implementation.

执行引擎仅指解释器(JIT、VM 等)或解释器和运行时环境的组合。编译器从不称为执行引擎(它不执行任何内容,只是转换为另一种语言),并且术语执行引擎很少用于指代静态编译的实现。

The event loop is part of the host environment, not the JavaScript implementation.

事件循环是宿主环境的一部分,而不是 JavaScript 实现。

回答by rajeshchauhan23102008

Javascript Runtime Environment

Javascript 运行时环境



  1. Provide various features/API's to build Javascript based software.
  2. It also includes a JS Engine(Interpreter/compiler).
  1. 提供各种功能/API 来构建基于 Javascript 的软件。
  2. 它还包括一个 JS 引擎(解释器/编译器)。

Here is the List of Runtime Environments

这是运行时环境列表

  • Browser: Provides DOMAPI, FetchAPI, Timer(setTimeout & setInterval), Storage(like Local Storage) etc.
  • Browser: 提供DOMAPI, FetchAPI, Timer( setTimeout & setInterval), Storage (like Local Storage) 等。

Example: Chrome, Firefox, Safari, Opera, Edgeetc

示例:Chrome、Firefox、Safari、Opera、Edge

  • Server Environment: Provides File SystemAccess, NetworkAccess, Console etc.
  • 服务器环境:提供文件系统访问、网络访问、控制台等。

Example: NodeJS, Deno

示例:NodeJS、Deno

  • Desktop Environment: Provides GUI API, File SystemAccess, NetworkAccess, Console etc.
  • 桌面环境:提供GUI API文件系统访问、网络访问、控制台等。

Example: Electron etc.

例如:电子等。

  • Mobile Environment:
  • 移动环境

Example: NativeScript, Ionic, PhoneGap, React Native etc

示例:NativeScript、Ionic、PhoneGap、React Native 等

NOTE: Event Loop is implemented in Runtime Environment

注意:事件循环是在运行时环境中实现的





Javascript Engine(Interpreter/compiler)

Javascript 引擎(解释器/编译器)



  1. Converts your Javascript code into machine language/code so that your computer(CPU) will execute it :)
  1. 将您的 Javascript 代码转换为机器语言/代码,以便您的计算机(CPU)执行它:)

Here is the List of Engines

这是引擎列表

  • Chrome V8: From Google
  • Chrome V8:来自谷歌

Used In: Used in Chrome Browser, NodeJS & in android based mobiles

用于:用于 Chrome 浏览器、NodeJS 和基于 android 的手机

  • SpiderMonkey: From Mozilla
  • SpiderMonkey: 来自 Mozilla

Used In: Used in Firefox Browser

用于:在火狐浏览器中使用

  • Nitro / JavascriptCore: From Apple
  • Nitro / JavascriptCore: 来自 Apple

Used In: Used in Safari Browser & in iOS based mobiles

用于:用于 Safari 浏览器和基于 iOS 的手机

  • Chakra & CharkraCore: From Microsoft
  • Chakra & CharkraCore: 来自微软

Used In: Used in Microsoft Edge Browser

用于:用于 Microsoft Edge 浏览器

----------

----------

Excellent Link for More Infomation

获取更多信息的优秀链接