javascript - Node 版本 8 中 Async/Await 的性能指南

标签 javascript node.js v8

async/await 在 node 版本 8 中可用。代码在 nodejs 中首次在 native 中是线性的。那很好。之前的许多文章声称,在 v8 javascript 引擎中,带有 try/catch block 的函数没有被优化。现在,async/await 需要 try/catch block 来处理错误。那么,作为开发人员需要做些什么来保持相同的性能?

最佳答案

try/catch 在提交 9aac80f 中收到 TurboFan 优化适用于 V8 5.3( Node v7.x 及更高版本)。这意味着 try/catch 性能不佳的历史说法不再成立。
来自 V8 blog post :

In the past V8’s had difficulties optimizing the kind of language features that are found in ES2015+. For example, it never became feasible to add exception handling (i.e. try/catch/finally) support to Crankshaft, V8’s classic optimizing compiler. This meant V8’s ability to optimize an ES6 feature like for...of, which essentially has an implicit finally clause, was limited. Crankshaft’s limitations and the overall complexity of adding new language features to full-codegen, V8’s baseline compiler, made it inherently difficult to ensure new ES features were added and optimized in V8 as quickly as they were standardized.

Fortunately, Ignition and TurboFan (V8’s new interpreter and compiler pipeline), were designed to support the entire JavaScript language from the beginning, including advanced control flow, exception handling, and most recently for...of and destructuring from ES2015. The tight integration of the architecture of Ignition and TurboFan make it possible to quickly add new features and to optimize them fast and incrementally.


async 函数中的

try/catch 只是 Promise .then.catch 的语法糖方法,因此性能由底层的 Promise 实现决定。 bluebird claims比原生 Promise 实现具有更好的性能,所以理论上 - 如果 Bluebird 声称是真的 - 通过用 Bluebird 的 Promise 实现覆盖原生 Promise 实现,您将获得更好的 try/catch 性能。
例如,在 Node 中:const Promise = require("bluebird")global.Promise = require("bluebird") 以全局覆盖它。

但是请注意,这可能会在未来发生变化,因为最初的 Promise 实现是在 JavaScript 中,但最近在 C++ 中重新实现,这可以在错误 #5343 中进行跟踪。 .

关于javascript - Node 版本 8 中 Async/Await 的性能指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481709/

相关文章:

c++ - 将 C++ 方法绑定(bind)到现有 JS 函数原型(prototype)的最简单方法?

javascript - 将文件存储在 Firebase 存储中

node.js:无法正确安装express

javascript - JavaScript 在执行 block 时会创建新的执行上下文吗?

node.js - 如何解析 Node 中的大型分隔文本文件

node.js - 使用 bluebird 进行 Node -redis promise 化

eclipse - 在 Eclipse 中运行和调试 Node 项目

javascript - 重绘 Canvas html5而不闪烁

javascript - 在 Canvas 中翻转 Sprite

javascript - Google 登录扩展 token