diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 3b614a7924f893..ecdcccc0640ff9 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -40,7 +40,9 @@ const { } = require('internal/errors').codes; const ReadableAsyncIterator = require('internal/streams/async_iterator'); const { emitExperimentalWarning } = require('internal/util'); -var StringDecoder; + +// Lazy loaded to improve the startup performance. +let StringDecoder; util.inherits(Readable, Stream); diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index a630cff717bcad..1ec3f208660910 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -14,7 +14,6 @@ const { ERR_MISSING_ARGS } } = require('internal/errors'); -const { StringDecoder } = require('string_decoder'); const EventEmitter = require('events'); const net = require('net'); const dgram = require('dgram'); @@ -47,6 +46,9 @@ const { const { SocketListSend, SocketListReceive } = SocketList; +// Lazy loaded for startup performance. +let StringDecoder; + const MAX_HANDLE_RETRANSMISSIONS = 3; // this object contain function to convert TCP objects to native handle objects @@ -476,6 +478,8 @@ function setupChannel(target, channel) { const control = new Control(channel); + if (StringDecoder === undefined) + StringDecoder = require('string_decoder').StringDecoder; var decoder = new StringDecoder('utf8'); var jsonBuffer = ''; var pendingHandle = null; diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index abf007de5cd623..4a523473804643 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -28,11 +28,13 @@ const { const assert = require('assert'); const LazyTransform = require('internal/streams/lazy_transform'); -const { StringDecoder } = require('string_decoder'); const { inherits } = require('util'); const { deprecate, normalizeEncoding } = require('internal/util'); +// Lazy loaded for startup performance. +let StringDecoder; + function rsaFunctionFor(method, defaultPadding) { return function(options, buffer) { const key = options.key || options; @@ -49,6 +51,8 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING); function getDecoder(decoder, encoding) { encoding = normalizeEncoding(encoding); + if (StringDecoder === undefined) + StringDecoder = require('string_decoder').StringDecoder; decoder = decoder || new StringDecoder(encoding); assert(decoder.encoding === encoding, 'Cannot change encoding'); return decoder; diff --git a/lib/readline.js b/lib/readline.js index 124fc8111b2f09..89dd1b84f2dde0 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -35,7 +35,6 @@ const { const { debug, inherits } = require('util'); const { Buffer } = require('buffer'); const EventEmitter = require('events'); -const { StringDecoder } = require('string_decoder'); const { CSI, emitKeys, @@ -52,6 +51,9 @@ const { kClearScreenDown } = CSI; +// Lazy load StringDecoder for startup performance. +let StringDecoder; + const kHistorySize = 30; const kMincrlfDelay = 100; // \r\n, \n, or \r followed by something other than \n @@ -73,6 +75,9 @@ function Interface(input, output, completer, terminal) { return new Interface(input, output, completer, terminal); } + if (StringDecoder === undefined) + StringDecoder = require('string_decoder').StringDecoder; + this._sawReturnAt = 0; this.isCompletionEnabled = true; this._sawKeyPress = false; @@ -987,6 +992,9 @@ Interface.prototype._ttyWrite = function(s, key) { function emitKeypressEvents(stream, iface) { if (stream[KEYPRESS_DECODER]) return; + + if (StringDecoder === undefined) + StringDecoder = require('string_decoder').StringDecoder; stream[KEYPRESS_DECODER] = new StringDecoder('utf8'); stream[ESCAPE_DECODER] = emitKeys(stream);