Skip to content

Commit be6a9d7

Browse files
committed
Revert "Revert "perf: reuse TextDecoder instance (nodejs#2863)" (nodejs#2999)"
This reverts commit 0ac82a0.
1 parent c7f9f62 commit be6a9d7

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

lib/web/websocket/receiver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const { WebsocketFrameSend } = require('./frame')
1212
// Copyright (c) 2013 Arnout Kazemier and contributors
1313
// Copyright (c) 2016 Luigi Pinca and contributors
1414

15+
const textDecoder = new TextDecoder('utf-8', { fatal: true })
16+
1517
class ByteParser extends Writable {
1618
#buffers = []
1719
#byteOffset = 0
@@ -314,8 +316,7 @@ class ByteParser extends Writable {
314316
}
315317

316318
try {
317-
// TODO: optimize this
318-
reason = new TextDecoder('utf-8', { fatal: true }).decode(reason)
319+
reason = textDecoder.decode(reason)
319320
} catch {
320321
return null
321322
}

lib/web/websocket/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ function fireEvent (e, target, eventConstructor = Event, eventInitDict = {}) {
6868
target.dispatchEvent(event)
6969
}
7070

71+
const textDecoder = new TextDecoder('utf-8', { fatal: true })
72+
7173
/**
7274
* @see https://websockets.spec.whatwg.org/#feedback-from-the-protocol
7375
* @param {import('./websocket').WebSocket} ws
@@ -87,7 +89,7 @@ function websocketMessageReceived (ws, type, data) {
8789
// -> type indicates that the data is Text
8890
// a new DOMString containing data
8991
try {
90-
dataForEvent = new TextDecoder('utf-8', { fatal: true }).decode(data)
92+
dataForEvent = textDecoder.decode(data)
9193
} catch {
9294
failWebsocketConnection(ws, 'Received invalid UTF-8 in text frame.')
9395
return

0 commit comments

Comments
 (0)