A cacheable dns.lookup(…) that respects the TTL 🎉

Build Status Coverage Status npm install size

Making lots of HTTP requests? You can save some time by caching DNS lookups.
Don't worry, this package respects the TTL 😃


const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();

http.get('', {lookup: cacheable.lookup}, response => {
    // Handle the response here


new CacheableLookup(options)

Returns a new instance of CacheableLookup.


Type: Object
Default: {}

Options used to cache the DNS lookups.


Type: Keyv adapter instance
Default: new Map()

A Keyv adapter which stores the cache.


Type: number
Default: Infinity

Limits the cache time (TTL).

If set to 0, it will make a new DNS query each time.


Type: Function
Default: new dns.Resolver()

An instance of DNS Resolver used to make DNS queries.

Entry object

Type: Object


Type: string

The IP address (can be an IPv4 or IPv6 address).


Type: number

The IP family (4 or 6).

CacheableLookup instance


Type: Array

DNS servers used to make the query. Can be overridden - then the new servers will be used.

lookup(hostname, options, callback)

lookupAsync(hostname, options)

The asynchronous version of dns.lookup(…).

Returns an entry object.
If options.all is true, returns an array of entry objects.

Note: If entry(ies) were not found, it will return undefined.


Type: string


Type: Object

The same as the dns.lookup(…) options.


Type: boolean
Default: false

Throw when there's no match.

If set to false and it gets no match, it will return undefined.

Note: This option is meant only for the asynchronous implementation! The synchronous version will always throw an error if no match found.


Type: boolean
Default: false

If true the entries returned by lookup(…) and lookupAsync(…) will have additional expires and ttl properties representing the expiration timestamp and the original TTL.

query(hostname, family)

An asynchronous function which returns cached DNS lookup entries. This is the base for lookupAsync(hostname, options) and lookup(hostname, options, callback).

Note: This function has no options.

Returns an array of objects with address, family, ttl and expires properties.

queryAndCache(hostname, family)

An asynchronous function which makes a new DNS lookup query and updates the database. This is used by query(hostname, family) if no entry in the database is present.

Returns an array of objects with address, family, ttl and expires properties.

High performance

See the benchmarks (queries localhost, performed on i7-7700k):

CacheableLookup#lookupAsync            x 219,396 ops/sec ±0.69%  (285 runs sampled)
CacheableLookup#lookup                 x 219,296 ops/sec ±0.20%  (284 runs sampled)
CacheableLookup#lookupAsync - zero TTL x 27.67   ops/sec ±51.06% (246 runs sampled)
CacheableLookup#lookup      - zero TTL x 29.64   ops/sec ±49.60% (204 runs sampled)
dns#resolve4                           x 27.03   ops/sec ±41.64% (246 runs sampled)
dns#lookup                             x 5,994   ops/sec ±0.26%  (285 runs sampled)
Fastest is CacheableLookup#lookup

The package is based on dns.resolve4(…) and dns.resolve6(…).

Why not dns.lookup(…)?

It is not possible to use dns.lookup(…) because underlying calls like getaddrinfo have no concept of servers or TTL (caching is done on OS level instead).