Skip to content

Commit 16aeddd

Browse files
BridgeARMylesBorins
authored andcommitted
lib: switch to Number.isNaN
Number.isNaN is now as fast as `val !== val`. Switch to the more readable version. Also switch all `isNaN` to `Number.isNaN`. PR-URL: #18744 Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f3ab106 commit 16aeddd

10 files changed

+27
-28
lines changed

.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ rules:
7474
message: __defineSetter__ is deprecated.
7575
no-return-await: error
7676
no-self-assign: error
77+
no-self-compare: error
7778
no-throw-literal: error
7879
no-unused-labels: error
7980
no-useless-call: error

lib/_stream_readable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function howMuchToRead(n, state) {
348348
return 0;
349349
if (state.objectMode)
350350
return 1;
351-
if (n !== n) {
351+
if (Number.isNaN(n)) {
352352
// Only flow one buffer at a time
353353
if (state.flowing && state.length)
354354
return state.buffer.head.data.length;

lib/buffer.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
362362
byteOffset = 0;
363363
} else {
364364
byteOffset = +byteOffset;
365-
// check for NaN
366-
if (byteOffset !== byteOffset)
365+
if (Number.isNaN(byteOffset))
367366
byteOffset = 0;
368367
}
369368

@@ -375,7 +374,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
375374
if (length === undefined) {
376375
length = maxLength;
377376
} else {
378-
// convert length to non-negative integer
377+
// Convert length to non-negative integer.
379378
length = +length;
380379
if (length > 0) {
381380
if (length > maxLength)
@@ -757,8 +756,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
757756
// Coerce to Number. Values like null and [] become 0.
758757
byteOffset = +byteOffset;
759758
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
760-
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
761-
if (byteOffset !== byteOffset) {
759+
if (Number.isNaN(byteOffset)) {
762760
byteOffset = dir ? 0 : buffer.length;
763761
}
764762
dir = !!dir; // Cast to bool.
@@ -989,15 +987,17 @@ function adjustOffset(offset, length) {
989987
// Use Math.trunc() to convert offset to an integer value that can be larger
990988
// than an Int32. Hence, don't use offset | 0 or similar techniques.
991989
offset = Math.trunc(offset);
992-
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
993-
if (offset === 0 || offset !== offset) {
990+
if (offset === 0) {
994991
return 0;
995-
} else if (offset < 0) {
992+
}
993+
if (offset < 0) {
996994
offset += length;
997995
return offset > 0 ? offset : 0;
998-
} else {
999-
return offset < length ? offset : length;
1000996
}
997+
if (offset < length) {
998+
return offset;
999+
}
1000+
return Number.isNaN(offset) ? 0 : length;
10011001
}
10021002

10031003

lib/events.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
5252
return defaultMaxListeners;
5353
},
5454
set: function(arg) {
55-
// check whether the input is a positive number (whose value is zero or
56-
// greater and not a NaN).
57-
if (typeof arg !== 'number' || arg < 0 || arg !== arg) {
55+
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
5856
const errors = lazyErrors();
5957
throw new errors.TypeError('ERR_OUT_OF_RANGE', 'defaultMaxListeners');
6058
}
@@ -76,7 +74,7 @@ EventEmitter.init = function() {
7674
// Obviously not all Emitters should be limited to 10. This function allows
7775
// that to be increased. Set to zero for unlimited.
7876
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
79-
if (typeof n !== 'number' || n < 0 || isNaN(n)) {
77+
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
8078
const errors = lazyErrors();
8179
throw new errors.TypeError('ERR_OUT_OF_RANGE', 'n');
8280
}

lib/internal/crypto/random.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { kMaxLength } = require('buffer');
1111
const kMaxUint32 = Math.pow(2, 32) - 1;
1212

1313
function assertOffset(offset, length) {
14-
if (typeof offset !== 'number' || offset !== offset) {
14+
if (typeof offset !== 'number' || Number.isNaN(offset)) {
1515
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'offset', 'number');
1616
}
1717

@@ -25,7 +25,7 @@ function assertOffset(offset, length) {
2525
}
2626

2727
function assertSize(size, offset = 0, length = Infinity) {
28-
if (typeof size !== 'number' || size !== size) {
28+
if (typeof size !== 'number' || Number.isNaN(size)) {
2929
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number');
3030
}
3131

lib/internal/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function createRepl(env, opts, cb) {
5151
}
5252

5353
const historySize = Number(env.NODE_REPL_HISTORY_SIZE);
54-
if (!isNaN(historySize) && historySize > 0) {
54+
if (!Number.isNaN(historySize) && historySize > 0) {
5555
opts.historySize = historySize;
5656
} else {
5757
// XXX(chrisdickinson): set here to avoid affecting existing applications

lib/net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ function createServerHandle(address, port, addressType, fd) {
12771277
handle = new Pipe(PipeConstants.SERVER);
12781278
if (process.platform === 'win32') {
12791279
var instances = parseInt(process.env.NODE_PENDING_PIPE_INSTANCES);
1280-
if (!isNaN(instances)) {
1280+
if (!Number.isNaN(instances)) {
12811281
handle.setPendingInstances(instances);
12821282
}
12831283
}

lib/readline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function Interface(input, output, completer, terminal) {
103103
}
104104

105105
if (typeof historySize !== 'number' ||
106-
isNaN(historySize) ||
106+
Number.isNaN(historySize) ||
107107
historySize < 0) {
108108
throw new errors.RangeError(
109109
'ERR_INVALID_OPT_VALUE',

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ function REPLServer(prompt,
476476
// display next prompt and return.
477477
if (trimmedCmd) {
478478
if (trimmedCmd.charAt(0) === '.' && trimmedCmd.charAt(1) !== '.' &&
479-
isNaN(parseFloat(trimmedCmd))) {
479+
Number.isNaN(parseFloat(trimmedCmd))) {
480480
const matches = trimmedCmd.match(/^\.([^\s]+)\s*(.*)$/);
481481
const keyword = matches && matches[1];
482482
const rest = matches && matches[2];

lib/zlib.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function Zlib(opts, mode) {
177177

178178
if (opts) {
179179
chunkSize = opts.chunkSize;
180-
if (chunkSize !== undefined && chunkSize === chunkSize) {
180+
if (chunkSize !== undefined && !Number.isNaN(chunkSize)) {
181181
if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize))
182182
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
183183
'chunkSize',
@@ -187,15 +187,15 @@ function Zlib(opts, mode) {
187187
}
188188

189189
flush = opts.flush;
190-
if (flush !== undefined && flush === flush) {
190+
if (flush !== undefined && !Number.isNaN(flush)) {
191191
if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush))
192192
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'flush', flush);
193193
} else {
194194
flush = Z_NO_FLUSH;
195195
}
196196

197197
finishFlush = opts.finishFlush;
198-
if (finishFlush !== undefined && finishFlush === finishFlush) {
198+
if (finishFlush !== undefined && !Number.isNaN(finishFlush)) {
199199
if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK ||
200200
!Number.isFinite(finishFlush)) {
201201
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@@ -207,7 +207,7 @@ function Zlib(opts, mode) {
207207
}
208208

209209
windowBits = opts.windowBits;
210-
if (windowBits !== undefined && windowBits === windowBits) {
210+
if (windowBits !== undefined && !Number.isNaN(windowBits)) {
211211
if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS ||
212212
!Number.isFinite(windowBits)) {
213213
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@@ -219,7 +219,7 @@ function Zlib(opts, mode) {
219219
}
220220

221221
level = opts.level;
222-
if (level !== undefined && level === level) {
222+
if (level !== undefined && !Number.isNaN(level)) {
223223
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL ||
224224
!Number.isFinite(level)) {
225225
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@@ -230,7 +230,7 @@ function Zlib(opts, mode) {
230230
}
231231

232232
memLevel = opts.memLevel;
233-
if (memLevel !== undefined && memLevel === memLevel) {
233+
if (memLevel !== undefined && !Number.isNaN(memLevel)) {
234234
if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL ||
235235
!Number.isFinite(memLevel)) {
236236
throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@@ -241,7 +241,7 @@ function Zlib(opts, mode) {
241241
}
242242

243243
strategy = opts.strategy;
244-
if (strategy !== undefined && strategy === strategy) {
244+
if (strategy !== undefined && !Number.isNaN(strategy)) {
245245
if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
246246
!Number.isFinite(strategy)) {
247247
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',

0 commit comments

Comments
 (0)