From 60eab9100fc6aea8c03339ecee220e2607fea244 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 6 May 2018 23:10:57 +0200 Subject: [PATCH] dns: lazy loaded PR-URL: /~https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/dgram.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 0a72834873e8bd..f3454ffc0702c4 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -35,7 +35,6 @@ const { ERR_SOCKET_DGRAM_NOT_RUNNING } = errors.codes; const { Buffer } = require('buffer'); -const dns = require('dns'); const util = require('util'); const { isUint8Array } = require('internal/util/types'); const EventEmitter = require('events'); @@ -47,6 +46,9 @@ const { UV_UDP_REUSEADDR } = process.binding('constants').os; const { UDP, SendWrap } = process.binding('udp_wrap'); +// Lazy load for startup performance. +let dns; + const BIND_STATE_UNBOUND = 0; const BIND_STATE_BINDING = 1; const BIND_STATE_BOUND = 2; @@ -72,9 +74,10 @@ function lookup6(lookup, address, callback) { function newHandle(type, lookup) { - if (lookup === undefined) + if (lookup === undefined) { + if (dns === undefined) dns = require('dns'); lookup = dns.lookup; - else if (typeof lookup !== 'function') + } else if (typeof lookup !== 'function') throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup); if (type === 'udp4') {