From 0ac82a06caec6d2ffcf62852d4cc9077a1223ea9 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 27 Mar 2024 10:10:44 +0100 Subject: [PATCH] Revert "perf: reuse TextDecoder instance (#2863)" (#2999) This reverts commit 2091dcfa1a9864dab7675be7c6bff34dbde06051. --- lib/web/websocket/receiver.js | 5 ++--- lib/web/websocket/util.js | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/web/websocket/receiver.js b/lib/web/websocket/receiver.js index ab6ed0bce02..108d8b08f75 100644 --- a/lib/web/websocket/receiver.js +++ b/lib/web/websocket/receiver.js @@ -12,8 +12,6 @@ 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 @@ -316,7 +314,8 @@ class ByteParser extends Writable { } try { - reason = textDecoder.decode(reason) + // TODO: optimize this + reason = new TextDecoder('utf-8', { fatal: true }).decode(reason) } catch { return null } diff --git a/lib/web/websocket/util.js b/lib/web/websocket/util.js index 4f0e4dcf5dd..6352b998ebd 100644 --- a/lib/web/websocket/util.js +++ b/lib/web/websocket/util.js @@ -68,8 +68,6 @@ 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 @@ -89,7 +87,7 @@ function websocketMessageReceived (ws, type, data) { // -> type indicates that the data is Text // a new DOMString containing data try { - dataForEvent = textDecoder.decode(data) + dataForEvent = new TextDecoder('utf-8', { fatal: true }).decode(data) } catch { failWebsocketConnection(ws, 'Received invalid UTF-8 in text frame.') return