JavaScript 引擎 V8迎来 8.4 版本

时间:2020-07-09 11:08:14 作者: gengting

 JavaScript 引擎 V8迎来 8.4 版本(测试阶段),正式版本将随 Chrome 84 一款推出。8.4 版本带来一些面向开发人员的特性。 

 主要亮点包括: 

 WebAssembly 

 缩短启动时间 

 WebAssembly 的基准编译器(Liftoff)现在支持原子指令和大容量内存操作。这意味着用户可以获得更快的启动时间。 

 更好的调试 

 为了不断改善 WebAssembly 的调试体验,开发团队表示现在能够检查任何暂停执行或到达断点的实时 WebAssembly 框架。这是通过重新使用 Liftoff 进行调试来实现的。过去,所有具有断点或逐步执行的代码都需要在 WebAssembly 解释器中执行,这大大降低了执行速度。使用 Liftoff 会损失大约三分之一的性能,但是可以单步执行所有代码,并随时进行检查。 

 SIMD Origin Trial 

 SIMD 提案使 WebAssembly 能够利用常用的硬件矢量指令来加速计算密集型工作负载。V8 支持 WebAssembly SIMD 提案。要在 Chrome 中启用此功能,请使用标记 chrome://flags/#enable-webassembly-simd 或注册 origin trial 试用。Origin 试用版允许开发人员在功能标准化之前进行试验,并提供反馈。 

 JavaScript 

 弱引用和终结器 

 提醒:弱引用和终结器是高级功能,它们取决于垃圾回收行为。垃圾回收是不确定的,可能根本不会发生。 

 JavaScript 是一种垃圾回收语言,这意味着当垃圾回收器运行时,程序无法再访问的对象所占用的内存可能会自动回收。除了 WeakMap 和 WeakSet 中的引用之外,JavaScript 中的所有引用都是强大的,可以防止对引用的对象进行垃圾回收。例如, 

 const globalRef = { 

 callback() { console.log('foo'); } 

 };// As long as globalRef is reachable through the global scope,// neither it nor the function in its callback property will be collected. 

 JavaScript 程序员现在可以通过 WeakRef 功能保留对象。如果弱引用所引用的对象也未得到强引用,则它们不会阻止对其进行垃圾回收。 

 const globalWeakRef = new WeakRef({ 

 callback() { console.log('foo'); } 

 }); 

 (async function() { 

 globalWeakRef.deref().callback(); // Logs “foo” to console. globalWeakRef is guaranteed to be alive 

 // for the first turn of the event loop after it was created. 

 await new Promise((resolve, reject) => { 

 setTimeout(() => { resolve('foo'); }, 42); 

 }); // Wait for a turn of the event loop. 

 globalWeakRef.deref()?.callback(); // The object inside globalWeakRef may be garbage collected 

 // after the first turn since it is not otherwise reachable.})(); 

 WeakRefs 的附带功能是 FinalizationRegistry,该功能使程序员可以在对象被垃圾回收后注册要调用的回调。例如,下面的程序可能在 IIFE 中回收了无法访问的对象后将日志记录到控制台 42。 

 const registry = new FinalizationRegistry((heldValue) => { console.log(heldValue); 

 }); 

 (function () { const garbage = {}; 

 registry.register(garbage, 42); // The second argument is the “held” value which gets passed 

 // to the finalizer when the first argument is garbage collected.})(); 

 结器计划在事件循环上运行,并且永不中断同步 JavaScript 的执行。 

 这些是高级且强大的功能,如果幸运的话,你的程序将不需要它们。 

 私有方法和访问器 

 v7.4 中提供的私有字段在对私有方法和访问器的支持中得到了完善。从句法上讲,私有方法和访问器的名称以 # 开头,就像私有字段一样。以下是语法的简要介绍。 

 class Component { #privateMethod() { console.log("I'm only callable inside Component!"); 

 } get #privateAccessor() { return 42; } set #privateAccessor(x) { } 

 } 

 私有方法和访问器具有与私有字段相同的作用域规则和语义。可参阅 说明 以了解更多信息。 

 V8 API 

 请使用 git log branch-heads/8.3..branch-heads/8.4 include/v8.h 来获取 API 更改列表。 

相关推荐
抖音

热文推荐

  • 48小时热文
  • 每周热文

JavaScript 引擎 V8迎来 8.4 版本