Skip to content

Commit ae2fd64

Browse files
committed
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`.
1 parent 7d42edc commit ae2fd64

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
@@ -340,7 +340,7 @@ function howMuchToRead(n, state) {
340340
return 0;
341341
if (state.objectMode)
342342
return 1;
343-
if (n !== n) {
343+
if (Number.isNaN(n)) {
344344
// Only flow one buffer at a time
345345
if (state.flowing && state.length)
346346
return state.buffer.head.data.length;

lib/buffer.js

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

@@ -376,7 +375,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
376375
if (length === undefined) {
377376
length = maxLength;
378377
} else {
379-
// convert length to non-negative integer
378+
// Convert length to non-negative integer.
380379
length = +length;
381380
if (length > 0) {
382381
if (length > maxLength)
@@ -760,8 +759,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
760759
// Coerce to Number. Values like null and [] become 0.
761760
byteOffset = +byteOffset;
762761
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
763-
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
764-
if (byteOffset !== byteOffset) {
762+
if (Number.isNaN(byteOffset)) {
765763
byteOffset = dir ? 0 : buffer.length;
766764
}
767765
dir = !!dir; // Cast to bool.
@@ -996,15 +994,17 @@ function adjustOffset(offset, length) {
996994
// Use Math.trunc() to convert offset to an integer value that can be larger
997995
// than an Int32. Hence, don't use offset | 0 or similar techniques.
998996
offset = Math.trunc(offset);
999-
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
1000-
if (offset === 0 || offset !== offset) {
997+
if (offset === 0) {
1001998
return 0;
1002-
} else if (offset < 0) {
999+
}
1000+
if (offset < 0) {
10031001
offset += length;
10041002
return offset > 0 ? offset : 0;
1005-
} else {
1006-
return offset < length ? offset : length;
10071003
}
1004+
if (offset < length) {
1005+
return offset;
1006+
}
1007+
return Number.isNaN(offset) ? 0 : length;
10081008
}
10091009

10101010

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.RangeError('ERR_OUT_OF_RANGE',
6058
'defaultMaxListeners',
@@ -79,7 +77,7 @@ EventEmitter.init = function() {
7977
// Obviously not all Emitters should be limited to 10. This function allows
8078
// that to be increased. Set to zero for unlimited.
8179
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
82-
if (typeof n !== 'number' || n < 0 || isNaN(n)) {
80+
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
8381
const errors = lazyErrors();
8482
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'n',
8583
'a non-negative number', n);

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
@@ -1304,7 +1304,7 @@ function createServerHandle(address, port, addressType, fd) {
13041304
handle = new Pipe(PipeConstants.SERVER);
13051305
if (process.platform === 'win32') {
13061306
var instances = parseInt(process.env.NODE_PENDING_PIPE_INSTANCES);
1307-
if (!isNaN(instances)) {
1307+
if (!Number.isNaN(instances)) {
13081308
handle.setPendingInstances(instances);
13091309
}
13101310
}

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
@@ -575,7 +575,7 @@ function REPLServer(prompt,
575575
// display next prompt and return.
576576
if (trimmedCmd) {
577577
if (trimmedCmd.charAt(0) === '.' && trimmedCmd.charAt(1) !== '.' &&
578-
isNaN(parseFloat(trimmedCmd))) {
578+
Number.isNaN(parseFloat(trimmedCmd))) {
579579
const matches = trimmedCmd.match(/^\.([^\s]+)\s*(.*)$/);
580580
const keyword = matches && matches[1];
581581
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)