Skip to content

Commit

Permalink
Revert "perf: reuse TextDecoder instance (nodejs#2863)" (nodejs#2999)
Browse files Browse the repository at this point in the history
This reverts commit 2091dcf.
  • Loading branch information
panva authored Mar 27, 2024
1 parent bf8c671 commit 0ac82a0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0ac82a0

Please sign in to comment.