节点版本8中的异步/等待性能指南 [英] Performance guidelines for Async/Await in Node Version 8
问题描述
async/await
在节点版本8中可用.该代码本来是在nodejs中首次线性执行.那很好.早先的许多文章声称,在v8 javascript引擎中,带有 try/catch
块的函数未经过优化.现在, async/await
需要 try/catch
块来处理错误.那么,作为开发人员,需要做些什么来保持相同的性能?
try/catch
在提交 V8博客文章:
过去,V8难以优化ES2015 +中提供的语言功能.例如,向V8的经典优化编译器Crankshaft添加异常处理(即
try/catch/finally
)支持从未变得可行.这意味着V8优化ES6功能(如for ... of
)的能力受到限制,该功能本质上具有隐式的finally子句.曲轴的局限性以及向V8基准编译器full-codegen中添加新语言功能的整体复杂性,使得固有的困难在于确保以标准化速度尽快在V8中添加和优化ES新功能.幸运的是,Ignition和TurboFan(V8的新解释器和编译器管道)从一开始就旨在支持整个JavaScript语言,包括高级控制流,异常处理以及最新的...代码>并从ES2015进行销毁.Ignition和TurboFan架构的紧密集成使快速添加新功能并快速,渐进地优化它们成为可能.
async
函数中的 try/catch
只是Promise .then
和 .catch
上的语法糖.因此,性能取决于底层的Promise实现.Bluebird 声明比本机Promise实现具有更好的性能,因此从理论上讲-如果Bluebird声明为真-通过使用Bluebird的Promise实现覆盖原生Promise实现,您将获得更好的 try/catch
性能.
例如,在节点中: const Promise = require("bluebird")
或 global.Promise = require("bluebird")
对其进行全局覆盖.>
但是请注意,这可能会在将来发生变化,因为最初的Promise实现是在JavaScript中实现的,但最近可以在C ++中重新实现,可以在bug 解决方案
try/catch
received TurboFan optimizations in commit 9aac80f
for V8 5.3
(Node v7.x
and above). This means that the historic statement that try/catch
has bad performance is no longer true.
From 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 likefor...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.
try/catch
in an async
function is just syntatic sugar over a Promise .then
and .catch
methods, and performance is therefore determined by the underlying Promise implementation. Bluebird claims to have better performance than native Promise implementations, so theoretically - if what Bluebird claims is true - you'll achieve better try/catch
performance by overriding the native Promise implementation with Bluebird's Promise implementation.
For example, in Node: const Promise = require("bluebird")
, or global.Promise = require("bluebird")
to override it globally.
Note however that this might change in the future, as the original Promise implementation was in JavaScript, but has recently been re-implemented in C++ as can be tracked in bug #5343.
这篇关于节点版本8中的异步/等待性能指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!