Skip to content

Commit cf071a0

Browse files
mcollinatargos
authored andcommitted
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. PR-URL: #28842 Fixes: #28586 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent e6b3bfe commit cf071a0

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
@@ -250,7 +250,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
250250
} else if (state.objectMode || chunk && chunk.length > 0) {
251251
if (typeof chunk !== 'string' &&
252252
!state.objectMode &&
253-
Object.getPrototypeOf(chunk) !== Buffer.prototype) {
253+
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
254+
!(chunk instanceof Buffer)) {
254255
chunk = Stream._uint8ArrayToBuffer(chunk);
255256
}
256257

@@ -393,7 +394,13 @@ function howMuchToRead(n, state) {
393394
// You can override either this method, or the async _read(n) below.
394395
Readable.prototype.read = function(n) {
395396
debug('read', n);
396-
n = parseInt(n, 10);
397+
// Same as parseInt(undefined, 10), however V8 7.3 performance regressed
398+
// in this scenario, so we are doing it manually.
399+
if (n === undefined) {
400+
n = NaN;
401+
} else if (!Number.isInteger(n)) {
402+
n = parseInt(n, 10);
403+
}
397404
const state = this._readableState;
398405
const nOrig = n;
399406

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 since V8 7.3.
281+
if (isBuf && !(chunk instanceof Buffer)) {
281282
chunk = Stream._uint8ArrayToBuffer(chunk);
282283
}
283284

0 commit comments

Comments
 (0)