gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:19:41 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com