Skip to content

Commit a6e108a

Browse files
committed
stream: resolve perf regression introduced by V8 7.3
This commit contains two fixes: 1. use instanceof instead of Object.getPrototypeOf, as checking an object prototype with Object.getPrototypeOf is slower than an instanceof check. 2. avoid parseInt(undefined, 10) to get NaN as it regressed. Fixes: nodejs#28586
1 parent 4daf153 commit a6e108a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/_stream_readable.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
256256
} else if (state.objectMode || chunk && chunk.length > 0) {
257257
if (typeof chunk !== 'string' &&
258258
!state.objectMode &&
259-
Object.getPrototypeOf(chunk) !== Buffer.prototype) {
259+
// Do not use Object.getPrototypeOf as it is slower.
260+
!(chunk instanceof Buffer)) {
260261
chunk = Stream._uint8ArrayToBuffer(chunk);
261262
}
262263

@@ -399,7 +400,13 @@ function howMuchToRead(n, state) {
399400
// You can override either this method, or the async _read(n) below.
400401
Readable.prototype.read = function(n) {
401402
debug('read', n);
402-
n = parseInt(n, 10);
403+
// Same as parseInt(undefined, 10), however V8 7.3 performance regressed
404+
// in this scenario, so we are doing it manually.
405+
if (n === undefined) {
406+
n = NaN;
407+
} else if (!Number.isInteger(n)) {
408+
n = parseInt(n, 10);
409+
}
403410
const state = this._readableState;
404411
const nOrig = n;
405412

lib/_stream_writable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ Writable.prototype.write = function(chunk, encoding, cb) {
277277
var ret = false;
278278
const isBuf = !state.objectMode && Stream._isUint8Array(chunk);
279279

280-
if (isBuf && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
280+
// Do not use Object.getPrototypeOf as it is slower.
281+
if (isBuf && !(chunk instanceof Buffer)) {
281282
chunk = Stream._uint8ArrayToBuffer(chunk);
282283
}
283284

0 commit comments

Comments
 (0)