V8 JavaScript 引擎

¥The V8 JavaScript Engine

V8 是支持 Google Chrome 的 JavaScript 引擎的名称。它是在使用 Chrome 浏览时获取我们的 JavaScript 并执行它的东西。

¥V8 is the name of the JavaScript engine that powers Google Chrome. It's the thing that takes our JavaScript and executes it while browsing with Chrome.

V8 是 JavaScript 引擎,即它解析和执行 JavaScript 代码。DOM 和其他 Web 平台 API(它们都构成了运行时环境)由浏览器提供。

¥V8 is the JavaScript engine i.e. it parses and executes JavaScript code. The DOM, and the other Web Platform APIs (they all makeup runtime environment) are provided by the browser.

很酷的是,JavaScript 引擎独立于托管它的浏览器。这一关键特性促成了 Node.js 的崛起。早在 2009 年,V8 就被选为 Node.js 的引擎,随着 Node.js 的流行度激增,V8 成为现在为大量用 JavaScript 编写的服务器端代码提供支持的引擎。

¥The cool thing is that the JavaScript engine is independent of the browser in which it's hosted. This key feature enabled the rise of Node.js. V8 was chosen to be the engine that powered Node.js back in 2009, and as the popularity of Node.js exploded, V8 became the engine that now powers an incredible amount of server-side code written in JavaScript.

Node.js 生态系统非常庞大,这要归功于 V8,它还为桌面应用和 Electron 等项目提供支持。

¥The Node.js ecosystem is huge and thanks to V8 which also powers desktop apps, with projects like Electron.

其他 JS 引擎

¥Other JS engines

其他浏览器有自己的 JavaScript 引擎:

¥Other browsers have their own JavaScript engine:

还有许多其他的。

¥and many others exist as well.

所有这些引擎都实现了 ECMA ES-262 标准,也称为 ECMAScript,这是 JavaScript 使用的标准。

¥All those engines implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript.

对性能的追求

¥The quest for performance

V8 用 C++ 编写,并且不断改进。它是可移植的,可在 Mac、Windows、Linux 和其他几个系统上运行。

¥V8 is written in C++, and it's continuously improved. It is portable and runs on Mac, Windows, Linux and several other systems.

在此 V8 介绍中,我们将忽略 V8 的实现细节:它们可以在更权威的网站上找到(例如 V8 官方网站),并且它们会随着时间的推移而变化,通常是彻底的。

¥In this V8 introduction, we will ignore the implementation details of V8: they can be found on more authoritative sites (e.g. the V8 official site), and they change over time, often radically.

V8 一直在不断发展,就像其他 JavaScript 引擎一样,以加速 Web 和 Node.js 生态系统。

¥V8 is always evolving, just like the other JavaScript engines around, to speed up the Web and the Node.js ecosystem.

在网络上,多年来一直存在着一场性能竞赛,我们(作为用户和开发者)从这场竞争中受益匪浅,因为我们年复一年地获得更快、更优化的机器。

¥On the web, there is a race for performance that's been going on for years, and we (as users and developers) benefit a lot from this competition because we get faster and more optimized machines year after year.

编译

¥Compilation

JavaScript 通常被认为是一种解释型语言,但现代 JavaScript 引擎不再只是解释 JavaScript,而是对其进行编译。

¥JavaScript is generally considered an interpreted language, but modern JavaScript engines no longer just interpret JavaScript, they compile it.

自 2009 年 SpiderMonkey JavaScript 编译器添加到 Firefox 3.5 以来,这种情况一直存在,每个人都遵循了这个想法。

¥This has been happening since 2009, when the SpiderMonkey JavaScript compiler was added to Firefox 3.5, and everyone followed this idea.

JavaScript 由 V8 内部编译,使用即时(JIT)编译来加快执行速度。

¥JavaScript is internally compiled by V8 with just-in-time (JIT) compilation to speed up the execution.

这似乎有悖常理,但自 2004 年推出 Google Maps 以来,JavaScript 已经从一种通常执行几十行代码的语言发展成为在浏览器中运行数千到数十万行代码的完整应用。

¥This might seem counter-intuitive, but since the introduction of Google Maps in 2004, JavaScript has evolved from a language that was generally executing a few dozens of lines of code to complete applications with thousands to hundreds of thousands of lines running in the browser.

我们的应用现在可以在浏览器中运行数小时,而不仅仅是一些表单验证规则或简单的脚本。

¥Our applications can now run for hours inside a browser, rather than being just a few form validation rules or simple scripts.

在这个新世界中,编译 JavaScript 非常有意义,因为虽然可能需要多花一点时间才能准备好 JavaScript,但一旦完成,它的性能将远远高于纯粹的解释代码。

¥In this new world, compiling JavaScript makes perfect sense because while it might take a little bit more to have the JavaScript ready, once done it's going to be much more performant than purely interpreted code.