Skip to content

Commit 0ace8f9

Browse files
BridgeARMylesBorins
authored andcommitted
string_decoder: lazy loaded
PR-URL: #20567 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 526163c commit 0ace8f9

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

lib/_stream_readable.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const {
4040
} = require('internal/errors').codes;
4141
const ReadableAsyncIterator = require('internal/streams/async_iterator');
4242
const { emitExperimentalWarning } = require('internal/util');
43-
var StringDecoder;
43+
44+
// Lazy loaded to improve the startup performance.
45+
let StringDecoder;
4446

4547
util.inherits(Readable, Stream);
4648

lib/internal/child_process.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
ERR_MISSING_ARGS
1515
}
1616
} = require('internal/errors');
17-
const { StringDecoder } = require('string_decoder');
1817
const EventEmitter = require('events');
1918
const net = require('net');
2019
const dgram = require('dgram');
@@ -47,6 +46,9 @@ const {
4746

4847
const { SocketListSend, SocketListReceive } = SocketList;
4948

49+
// Lazy loaded for startup performance.
50+
let StringDecoder;
51+
5052
const MAX_HANDLE_RETRANSMISSIONS = 3;
5153

5254
// this object contain function to convert TCP objects to native handle objects
@@ -476,6 +478,8 @@ function setupChannel(target, channel) {
476478

477479
const control = new Control(channel);
478480

481+
if (StringDecoder === undefined)
482+
StringDecoder = require('string_decoder').StringDecoder;
479483
var decoder = new StringDecoder('utf8');
480484
var jsonBuffer = '';
481485
var pendingHandle = null;

lib/internal/crypto/cipher.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ const {
2828

2929
const assert = require('assert');
3030
const LazyTransform = require('internal/streams/lazy_transform');
31-
const { StringDecoder } = require('string_decoder');
3231

3332
const { inherits } = require('util');
3433
const { deprecate, normalizeEncoding } = require('internal/util');
3534

35+
// Lazy loaded for startup performance.
36+
let StringDecoder;
37+
3638
function rsaFunctionFor(method, defaultPadding) {
3739
return function(options, buffer) {
3840
const key = options.key || options;
@@ -49,6 +51,8 @@ const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING);
4951

5052
function getDecoder(decoder, encoding) {
5153
encoding = normalizeEncoding(encoding);
54+
if (StringDecoder === undefined)
55+
StringDecoder = require('string_decoder').StringDecoder;
5256
decoder = decoder || new StringDecoder(encoding);
5357
assert(decoder.encoding === encoding, 'Cannot change encoding');
5458
return decoder;

lib/readline.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535
const { debug, inherits } = require('util');
3636
const { Buffer } = require('buffer');
3737
const EventEmitter = require('events');
38-
const { StringDecoder } = require('string_decoder');
3938
const {
4039
CSI,
4140
emitKeys,
@@ -52,6 +51,9 @@ const {
5251
kClearScreenDown
5352
} = CSI;
5453

54+
// Lazy load StringDecoder for startup performance.
55+
let StringDecoder;
56+
5557
const kHistorySize = 30;
5658
const kMincrlfDelay = 100;
5759
// \r\n, \n, or \r followed by something other than \n
@@ -73,6 +75,9 @@ function Interface(input, output, completer, terminal) {
7375
return new Interface(input, output, completer, terminal);
7476
}
7577

78+
if (StringDecoder === undefined)
79+
StringDecoder = require('string_decoder').StringDecoder;
80+
7681
this._sawReturnAt = 0;
7782
this.isCompletionEnabled = true;
7883
this._sawKeyPress = false;
@@ -987,6 +992,9 @@ Interface.prototype._ttyWrite = function(s, key) {
987992

988993
function emitKeypressEvents(stream, iface) {
989994
if (stream[KEYPRESS_DECODER]) return;
995+
996+
if (StringDecoder === undefined)
997+
StringDecoder = require('string_decoder').StringDecoder;
990998
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
991999

9921000
stream[ESCAPE_DECODER] = emitKeys(stream);

0 commit comments

Comments
 (0)