Why some TCP sockets in Node.js take much more time to connect than most others?

I’ve created small lib tcp-exists to check if tcp endpoint exists. Just to iterate over a network to find anything on special ports.

code of lib on github

But when I am iterating – some of tcp sockets (which creates by lib tcp-exists) take a large amount of time (1-3s instead of 5-20ms) to establish connections on 100% existing endpoints.

import tcpExists from 'tcp-exists'

async function main () {
  for (let i = 1; i < 250; ++i) {
    let time = performance.now()
    const exists = await tcpExists('', 80, 2000)
    time = performance.now() - time

    console.log(`time [${i}]:`, time)
    if (time > 500) console.log('=====STRANGE=====')
    if (!exists) console.log('exists')


There is no difference between am I checking the same port or different ports on one IP, or am I checking different IPs. Some of that connections will take x100 more time to establish a connection. It can lead to a situation when there is an endpoint that exists, but the function will return false because of a small timeout.

I cannot set a timeout bigger than 25ms for one check. Because if I will want to scan from to (65536 endpoints) — where are most of IP has no endpoint and should be returned false by timeout — and use for example 1s as timeout – then it will take 65536 seconds or ~18hours…

Source: JavaSript – Stack Overflow

October 26, 2021
Category : News
Tags: javascript | node.js | sockets | tcp

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.