Skip to content

Commit cab08f4

Browse files
committed
lib: refactor slice usage to subarray
Signed-off-by: hainenber <[email protected]>
1 parent 1e1f455 commit cab08f4

File tree

6 files changed

+8
-9
lines changed

6 files changed

+8
-9
lines changed

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
10721072
if (server.listenerCount(eventName) > 0) {
10731073
debug('SERVER have listener for %s', eventName);
10741074

1075-
const bodyHead = d.slice(ret, d.length);
1075+
const bodyHead = d.subarray(ret, d.length);
10761076

10771077
if (req.complete && socket[kUpgradeStream]) {
10781078
// Previously emitted, now completed - just activate the stream

lib/internal/streams/readable.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,9 +1620,8 @@ function fromList(n, state) {
16201620
}
16211621
}
16221622
} else if (n < buf[idx].length) {
1623-
// `slice` is the same for buffers and strings.
1624-
ret = buf[idx].slice(0, n);
1625-
buf[idx] = buf[idx].slice(n);
1623+
ret = typeof buf[idx] === 'string' ? buf[idx].slice(0, n) : buf[idx].subarray(0, n);
1624+
buf[idx] = typeof buf[idx] === 'string' ? buf[idx].slice(n) : buf[idx].subarray(n);
16261625
} else if (n === buf[idx].length) {
16271626
// First chunk is a perfect match.
16281627
ret = buf[idx];

lib/internal/test_runner/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class FileTest extends Test {
330330
while (bufferHead && headerIndex !== 0) {
331331
const nonSerializedData = headerIndex === -1 ?
332332
bufferHead :
333-
bufferHead.slice(0, headerIndex);
333+
bufferHead.subarray(0, headerIndex);
334334
nonSerialized = Buffer.concat([nonSerialized, nonSerializedData]);
335335
this.#rawBufferSize -= TypedArrayPrototypeGetLength(nonSerializedData);
336336
if (headerIndex === -1) {

lib/zlib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function processChunkSync(self, chunk, flushFlag) {
440440

441441
const have = availOutBefore - availOutAfter;
442442
if (have > 0) {
443-
const out = buffer.slice(offset, offset + have);
443+
const out = buffer.subarray(offset, offset + have);
444444
offset += have;
445445
buffers.push(out);
446446
nread += out.byteLength;
@@ -525,7 +525,7 @@ function processCallback() {
525525
const have = handle.availOutBefore - availOutAfter;
526526
let streamBufferIsFull = false;
527527
if (have > 0) {
528-
const out = self._outBuffer.slice(self._outOffset, self._outOffset + have);
528+
const out = self._outBuffer.subarray(self._outOffset, self._outOffset + have);
529529
self._outOffset += have;
530530
streamBufferIsFull = !self.push(out);
531531
} else {

test/parallel/test-buffer-nopendingdep-map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ process.on('warning', common.mustNotCall('A warning should not be emitted'));
1010

1111
Buffer.from('abc').map((i) => i);
1212
Buffer.from('abc').filter((i) => i);
13-
Buffer.from('abc').slice(1, 2);
13+
Buffer.from('abc').subarray(1, 2);

test/parallel/test-zlib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SlowStream extends stream.Stream {
140140
return this.emit('end');
141141
}
142142
const end = Math.min(this.offset + this.trickle, this.length);
143-
const c = this.chunk.slice(this.offset, end);
143+
const c = this.chunk.subarray(this.offset, end);
144144
this.offset += c.length;
145145
this.emit('data', c);
146146
process.nextTick(emit);

0 commit comments

Comments
 (0)