From c7df1c1aff08603c39c76f42912c9a832b43c594 Mon Sep 17 00:00:00 2001 From: Matt Weber <1062734+mweberxyz@users.noreply.github.com> Date: Fri, 29 Mar 2024 01:57:14 -0400 Subject: [PATCH] Revert "Revert "perf: reuse TextDecoder instance (#2863)" (#2999)" This reverts commit 0ac82a06caec6d2ffcf62852d4cc9077a1223ea9. --- lib/web/websocket/receiver.js | 5 +++-- lib/web/websocket/util.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/web/websocket/receiver.js b/lib/web/websocket/receiver.js index 108d8b08f75..ab6ed0bce02 100644 --- a/lib/web/websocket/receiver.js +++ b/lib/web/websocket/receiver.js @@ -12,6 +12,8 @@ const { WebsocketFrameSend } = require('./frame') // Copyright (c) 2013 Arnout Kazemier and contributors // Copyright (c) 2016 Luigi Pinca and contributors +const textDecoder = new TextDecoder('utf-8', { fatal: true }) + class ByteParser extends Writable { #buffers = [] #byteOffset = 0 @@ -314,8 +316,7 @@ class ByteParser extends Writable { } try { - // TODO: optimize this - reason = new TextDecoder('utf-8', { fatal: true }).decode(reason) + reason = textDecoder.decode(reason) } catch { return null } diff --git a/lib/web/websocket/util.js b/lib/web/websocket/util.js index 6352b998ebd..4f0e4dcf5dd 100644 --- a/lib/web/websocket/util.js +++ b/lib/web/websocket/util.js @@ -68,6 +68,8 @@ function fireEvent (e, target, eventConstructor = Event, eventInitDict = {}) { target.dispatchEvent(event) } +const textDecoder = new TextDecoder('utf-8', { fatal: true }) + /** * @see https://websockets.spec.whatwg.org/#feedback-from-the-protocol * @param {import('./websocket').WebSocket} ws @@ -87,7 +89,7 @@ function websocketMessageReceived (ws, type, data) { // -> type indicates that the data is Text // a new DOMString containing data try { - dataForEvent = new TextDecoder('utf-8', { fatal: true }).decode(data) + dataForEvent = textDecoder.decode(data) } catch { failWebsocketConnection(ws, 'Received invalid UTF-8 in text frame.') return