Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reuse TextDecoder instance #2863

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -322,8 +324,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
}
Expand Down
4 changes: 3 additions & 1 deletion lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
Expand All @@ -74,7 +76,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
Expand Down