JavaScript 引擎V8 發布8.4 版本
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更改列表。