Skip to content

Commit 9b21cbd

Browse files
committed
stream: consolidate TextEncoder to single instance in utils
Four separate `TextEncoder` instances were created across `from.js`, `pull.js`, `broadcast.js`, and `utils.js`. Consolidate to the single instance in `utils.js` by having all files use the existing `toUint8Array()` helper for `string`-to-`Uint8Array` conversion.
1 parent 88964ab commit 9b21cbd

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

lib/internal/streams/iter/broadcast.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const {
2222
TypedArrayPrototypeGetByteLength,
2323
} = primordials;
2424

25-
const { TextEncoder } = require('internal/encoding');
26-
2725
const { isError, lazyDOMException } = require('internal/util');
2826

2927
const {
@@ -49,14 +47,13 @@ const {
4947

5048
const {
5149
allUint8Array,
50+
toUint8Array,
5251
} = require('internal/streams/iter/utils');
5352

5453
const {
5554
RingBuffer,
5655
} = require('internal/streams/iter/ringbuffer');
5756

58-
const encoder = new TextEncoder();
59-
6057
// Cached resolved promise to avoid allocating a new one on every sync fast-path.
6158
const kResolvedPromise = PromiseResolve();
6259

@@ -416,7 +413,7 @@ class BroadcastWriter {
416413
if (!options?.signal && !this.#closed && !this.#aborted &&
417414
this.#broadcast[kCanWrite]()) {
418415
const converted =
419-
typeof chunk === 'string' ? encoder.encode(chunk) : chunk;
416+
toUint8Array(chunk);
420417
this.#broadcast[kWrite]([converted]);
421418
this.#totalBytes += TypedArrayPrototypeGetByteLength(converted);
422419
return kResolvedPromise;
@@ -431,7 +428,7 @@ class BroadcastWriter {
431428
const converted = allUint8Array(chunks) ?
432429
ArrayPrototypeSlice(chunks) :
433430
ArrayPrototypeMap(chunks, (c) =>
434-
(typeof c === 'string' ? encoder.encode(c) : c));
431+
toUint8Array(c));
435432
this.#broadcast[kWrite](converted);
436433
for (let i = 0; i < converted.length; i++) {
437434
this.#totalBytes += TypedArrayPrototypeGetByteLength(converted[i]);
@@ -456,7 +453,7 @@ class BroadcastWriter {
456453
const converted = allUint8Array(chunks) ?
457454
ArrayPrototypeSlice(chunks) :
458455
ArrayPrototypeMap(chunks, (c) =>
459-
(typeof c === 'string' ? encoder.encode(c) : c));
456+
toUint8Array(c));
460457

461458
if (this.#broadcast[kWrite](converted)) {
462459
for (let i = 0; i < converted.length; i++) {
@@ -485,7 +482,7 @@ class BroadcastWriter {
485482
if (this.#closed || this.#aborted) return false;
486483
if (!this.#broadcast[kCanWrite]()) return false;
487484
const converted =
488-
typeof chunk === 'string' ? encoder.encode(chunk) : chunk;
485+
toUint8Array(chunk);
489486
if (this.#broadcast[kWrite]([converted])) {
490487
this.#totalBytes += TypedArrayPrototypeGetByteLength(converted);
491488
return true;
@@ -499,7 +496,7 @@ class BroadcastWriter {
499496
const converted = allUint8Array(chunks) ?
500497
ArrayPrototypeSlice(chunks) :
501498
ArrayPrototypeMap(chunks, (c) =>
502-
(typeof c === 'string' ? encoder.encode(c) : c));
499+
toUint8Array(c));
503500
if (this.#broadcast[kWrite](converted)) {
504501
for (let i = 0; i < converted.length; i++) {
505502
this.#totalBytes += TypedArrayPrototypeGetByteLength(converted[i]);

lib/internal/streams/iter/from.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727
ERR_INVALID_ARG_TYPE,
2828
},
2929
} = require('internal/errors');
30-
const { TextEncoder } = require('internal/encoding');
30+
3131
const {
3232
isArrayBuffer,
3333
isPromise,
@@ -39,8 +39,9 @@ const {
3939
toAsyncStreamable,
4040
} = require('internal/streams/iter/types');
4141

42-
// Shared TextEncoder instance for string conversion.
43-
const encoder = new TextEncoder();
42+
const {
43+
toUint8Array,
44+
} = require('internal/streams/iter/utils');
4445

4546
// Maximum number of chunks to yield per batch from from(Uint8Array[]).
4647
// Bounds peak memory when arrays flow through transforms, which must
@@ -149,7 +150,7 @@ function hasToPrimitive(obj) {
149150
*/
150151
function primitiveToUint8Array(chunk) {
151152
if (typeof chunk === 'string') {
152-
return encoder.encode(chunk);
153+
return toUint8Array(chunk);
153154
}
154155
if (isArrayBuffer(chunk)) {
155156
return new Uint8Array(chunk);
@@ -233,7 +234,7 @@ function* normalizeSyncValue(value) {
233234
if (typeof value === 'object' && value !== null) {
234235
const str = tryStringCoercion(value);
235236
if (str !== null) {
236-
yield encoder.encode(str);
237+
yield toUint8Array(str);
237238
return;
238239
}
239240
}
@@ -357,7 +358,7 @@ async function* normalizeAsyncValue(value) {
357358
if (typeof value === 'object' && value !== null) {
358359
const str = tryStringCoercion(value);
359360
if (str !== null) {
360-
yield encoder.encode(str);
361+
yield toUint8Array(str);
361362
return;
362363
}
363364
}

lib/internal/streams/iter/pull.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const {
2222
ERR_OPERATION_FAILED,
2323
},
2424
} = require('internal/errors');
25-
const { TextEncoder } = require('internal/encoding');
2625
const { isError, lazyDOMException } = require('internal/util');
2726
const {
2827
isPromise,
@@ -41,11 +40,9 @@ const {
4140
const {
4241
isPullOptions,
4342
parsePullArgs,
43+
toUint8Array,
4444
} = require('internal/streams/iter/utils');
4545

46-
// Shared TextEncoder instance for string conversion.
47-
const encoder = new TextEncoder();
48-
4946
// =============================================================================
5047
// Type Guards and Helpers
5148
// =============================================================================
@@ -126,7 +123,7 @@ function* flattenTransformYieldSync(value) {
126123
return;
127124
}
128125
if (typeof value === 'string') {
129-
yield encoder.encode(value);
126+
yield toUint8Array(value);
130127
return;
131128
}
132129
// Must be Iterable<TransformYield>
@@ -149,7 +146,7 @@ async function* flattenTransformYieldAsync(value) {
149146
return;
150147
}
151148
if (typeof value === 'string') {
152-
yield encoder.encode(value);
149+
yield toUint8Array(value);
153150
return;
154151
}
155152
// Check for async iterable first

0 commit comments

Comments
 (0)