I note that the time it takes for a client web socket to open is around 2 seconds. This appears to be true for the first as well as subsequent socket connections. The issue appears to be with Deno.serve where the handler callback is taking an unusual amount of time to raise the callback when the client is a WebSocket.
Reproduction
// simple server
await Deno.serve({ port: 5000 }, request => {
const { response, socket } = Deno.upgradeWebSocket(request)
socket.addEventListener('open', () => console.log('server open'))
return response
})
// (optional) defer connection to allow server to initialize
await new Promise(resolve => setTimeout(resolve, 4000))
// measure time till client open
console.time('client open')
const socket = new WebSocket('ws://localhost:5000')
socket.addEventListener('open', () => console.timeEnd('client open'))
// client open: 2031ms ?
Browser
Establishing WebSocket connections from a browser seems to be marginally better, but still unusually slow with average connections to localhost measuring in around 300ms. The following was measured from Edge.
client open: 319.58984375 ms // edge
Additional
As noted, the issue seems to be related to the Deno.serve callback taking too long to invoke when the caller is a WebSocket. The Deno.upgradeWebSocket call does seem to be fast.
await Deno.serve({ port: 5000 }, request => { // slow - when the client is a WebSocket
const { response, socket } = Deno.upgradeWebSocket(request) // fast
})
System Info
operating system: Windows 10 deno 1.45.5 (release, x86_64-pc-windows-msvc) v8 12.7.224.13 typescript 5.5.2
Overview
I note that the time it takes for a client web socket to open is around 2 seconds. This appears to be true for the first as well as subsequent socket connections. The issue appears to be with Deno.serve where the handler callback is taking an unusual amount of time to raise the callback when the client is a WebSocket.
Reproduction
Browser
Establishing WebSocket connections from a browser seems to be marginally better, but still unusually slow with average connections to localhost measuring in around 300ms. The following was measured from Edge.
Additional
As noted, the issue seems to be related to the Deno.serve callback taking too long to invoke when the caller is a WebSocket. The Deno.upgradeWebSocket call does seem to be fast.