Node.js 22啟用內建WebSocket客戶端,並透過require()強化ES模組支援
· 2024-04-25

Node.js 22允許開發者透過require()來整合ES模組,還預設啟用了內建WebSocket實作,讓開發者不再需要依賴第三方模組,該版本將會在10月成為長期支援版本

開源跨平臺JavaScript執行環境Node.js推出第22主要版本,這個新版本加入require()對ECMAScript(ES)模組的支援,更新V8 JavaScript引擎,同時也預設啟用內建的WebSocket實作。Node.js 22目前以Current版本提供,在10月時則轉為長期支援版本。

Node.js 22版本新增了require()對ES模組的支援,這代表著Node.js在模組系統互通性的改進。這項實驗性功能允許開發者在使用CommonJS模組系統的同時,透過require()來整合ES模組。開發團隊指出,該功能行為類似動態import(),但是具有同步操作的便利性,對於需要嚴格確認相依項目載入順序的應用來說特別重要。

Node.js計畫未來將require()對同步ES模組的支援設為預設行為,以進一步提升Node.js應用的模組互通性,同時也是推動JavaScript標準化與強化開發者體驗的積極作為。

而在這個新版本中,另一個值得注意的更新是預設啟用與瀏覽器相容的WebSocket實作。WebSocket是一種網路通訊協定,能夠開啟一個持久連接,使得伺服器和客戶端可以隨時發送資料給對方,這種方式適合需要即時交換資料的應用,像是聊天室與遊戲等。

在過去的Node.js版本,要使用WebSocket需要仰賴外部第三方函式庫,但是在Node.js 22中,內建WebSocket客戶端實作已經正式啟用,代表開發者可以直接使用這個功能,不需要安裝額外的模組。這樣的改進簡化了開發過程,並且減少對外部相依項目的依賴。

Node.js所使用的V8引擎更新至12.4版本,新增功能包括WebAssembly垃圾回收、Array.fromAsync和Set方法等。而V8也使用了一個新的編譯器,現在於Node.js更新中預設啟用,該編譯器能夠提升JavaScript執行效率,特別像是打包工具、格式化程式或各種建置腳本等短時效CLI程式,皆有助於改善執行效能。

開發團隊這次更新也提高了Node.js中的High Water Mark,從原本的16 KB提高到64 KB,這代表Node.js串流可以在記憶體中儲存更多的資料。High Water Mark是Node.js串流的重要配置參數,其指定串流在內部緩衝取可以儲存多少資料,是一個用於平衡效能和記憶體使用的重要參數。

High Water Mark參數從16 KB提高至64 KB,雖然代表著有助於提升資料處理速度,但是也意味Node.js應用可能會使用更多記憶體來快取資料,對於記憶體資源有限的環境,開發者可依需求明確設定setDefaultHighWaterMark,以限制串流緩衝大小。

Popular articles
FDJ completes Kindred deal to transform into ‘Europe’s champion’
Sports Betting
Online gambling, crypto pose ongoing money laundering risks in Philippines, analyst says
Southeast Asia
FDJ says it doesn’t foresee French gambling tax hike, as stock price hit
Sports Betting
NFL player calls out NFL fans upset about bets: ‘We’re human too’
Sports Betting
Vietnam's tightening online gaming policy creates new market opportunities
Southeast Asia
Crypto in gambling: Market overview 2024
Marketing
Swedish lawmaker proposes lowered gambling tax to fight black market
Sports Betting
GeoComply report: Betting while at NFL games soaring so far this season
Sports Betting
Colombia to hike online gambling tax rate
Regulation
GamingTECH CEE Awards 2025: The Online Voting Battle Begins February 12!
Online Casino
In a rare video message, Light & Wonder CEO says slot issue was ‘an isolated incident’
Regulation
The ultimate gambler? How Bet365’s Denise Coates became Britain’s richest woman
Sports Betting
Underreported taxes on bettors’ gambling winnings leaves IRS $1.4 billion short
Sports Betting
Irish lawmakers at odds over change in gambling bill allowing ‘inducements’
Sports Betting
Netherlands goverment proses raising the minimum age for online casino slots
Regulation
Home
Game
Cooperation
Find
My