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: https://github.com/nodejs/node/pull/18744
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-02-13 00:56:35 +01:00
parent 43b8ce4ce7
commit 96c57fbfaa
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
10 changed files with 27 additions and 28 deletions

View File

@ -74,6 +74,7 @@ rules:
message: __defineSetter__ is deprecated. message: __defineSetter__ is deprecated.
no-return-await: error no-return-await: error
no-self-assign: error no-self-assign: error
no-self-compare: error
no-throw-literal: error no-throw-literal: error
no-unused-labels: error no-unused-labels: error
no-useless-call: error no-useless-call: error

View File

@ -341,7 +341,7 @@ function howMuchToRead(n, state) {
return 0; return 0;
if (state.objectMode) if (state.objectMode)
return 1; return 1;
if (n !== n) { if (Number.isNaN(n)) {
// Only flow one buffer at a time // Only flow one buffer at a time
if (state.flowing && state.length) if (state.flowing && state.length)
return state.buffer.head.data.length; return state.buffer.head.data.length;

View File

@ -363,8 +363,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
byteOffset = 0; byteOffset = 0;
} else { } else {
byteOffset = +byteOffset; byteOffset = +byteOffset;
// check for NaN if (Number.isNaN(byteOffset))
if (byteOffset !== byteOffset)
byteOffset = 0; byteOffset = 0;
} }
@ -376,7 +375,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
if (length === undefined) { if (length === undefined) {
length = maxLength; length = maxLength;
} else { } else {
// convert length to non-negative integer // Convert length to non-negative integer.
length = +length; length = +length;
if (length > 0) { if (length > 0) {
if (length > maxLength) if (length > maxLength)
@ -760,8 +759,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
// Coerce to Number. Values like null and [] become 0. // Coerce to Number. Values like null and [] become 0.
byteOffset = +byteOffset; byteOffset = +byteOffset;
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer. // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
// `x !== x`-style conditionals are a faster form of `isNaN(x)` if (Number.isNaN(byteOffset)) {
if (byteOffset !== byteOffset) {
byteOffset = dir ? 0 : buffer.length; byteOffset = dir ? 0 : buffer.length;
} }
dir = !!dir; // Cast to bool. dir = !!dir; // Cast to bool.
@ -996,15 +994,17 @@ function adjustOffset(offset, length) {
// Use Math.trunc() to convert offset to an integer value that can be larger // Use Math.trunc() to convert offset to an integer value that can be larger
// than an Int32. Hence, don't use offset | 0 or similar techniques. // than an Int32. Hence, don't use offset | 0 or similar techniques.
offset = Math.trunc(offset); offset = Math.trunc(offset);
// `x !== x`-style conditionals are a faster form of `isNaN(x)` if (offset === 0) {
if (offset === 0 || offset !== offset) {
return 0; return 0;
} else if (offset < 0) { }
if (offset < 0) {
offset += length; offset += length;
return offset > 0 ? offset : 0; return offset > 0 ? offset : 0;
} else {
return offset < length ? offset : length;
} }
if (offset < length) {
return offset;
}
return Number.isNaN(offset) ? 0 : length;
} }

View File

@ -52,9 +52,7 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
return defaultMaxListeners; return defaultMaxListeners;
}, },
set: function(arg) { set: function(arg) {
// check whether the input is a positive number (whose value is zero or if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
// greater and not a NaN).
if (typeof arg !== 'number' || arg < 0 || arg !== arg) {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.RangeError('ERR_OUT_OF_RANGE', throw new errors.RangeError('ERR_OUT_OF_RANGE',
'defaultMaxListeners', 'defaultMaxListeners',
@ -79,7 +77,7 @@ EventEmitter.init = function() {
// Obviously not all Emitters should be limited to 10. This function allows // Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited. // that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || isNaN(n)) { if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'n', throw new errors.RangeError('ERR_OUT_OF_RANGE', 'n',
'a non-negative number', n); 'a non-negative number', n);

View File

@ -11,7 +11,7 @@ const { kMaxLength } = require('buffer');
const kMaxUint32 = Math.pow(2, 32) - 1; const kMaxUint32 = Math.pow(2, 32) - 1;
function assertOffset(offset, length) { function assertOffset(offset, length) {
if (typeof offset !== 'number' || offset !== offset) { if (typeof offset !== 'number' || Number.isNaN(offset)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'offset', 'number'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'offset', 'number');
} }
@ -25,7 +25,7 @@ function assertOffset(offset, length) {
} }
function assertSize(size, offset = 0, length = Infinity) { function assertSize(size, offset = 0, length = Infinity) {
if (typeof size !== 'number' || size !== size) { if (typeof size !== 'number' || Number.isNaN(size)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number'); throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number');
} }

View File

@ -51,7 +51,7 @@ function createRepl(env, opts, cb) {
} }
const historySize = Number(env.NODE_REPL_HISTORY_SIZE); const historySize = Number(env.NODE_REPL_HISTORY_SIZE);
if (!isNaN(historySize) && historySize > 0) { if (!Number.isNaN(historySize) && historySize > 0) {
opts.historySize = historySize; opts.historySize = historySize;
} else { } else {
// XXX(chrisdickinson): set here to avoid affecting existing applications // XXX(chrisdickinson): set here to avoid affecting existing applications

View File

@ -1304,7 +1304,7 @@ function createServerHandle(address, port, addressType, fd) {
handle = new Pipe(PipeConstants.SERVER); handle = new Pipe(PipeConstants.SERVER);
if (process.platform === 'win32') { if (process.platform === 'win32') {
var instances = parseInt(process.env.NODE_PENDING_PIPE_INSTANCES); var instances = parseInt(process.env.NODE_PENDING_PIPE_INSTANCES);
if (!isNaN(instances)) { if (!Number.isNaN(instances)) {
handle.setPendingInstances(instances); handle.setPendingInstances(instances);
} }
} }

View File

@ -103,7 +103,7 @@ function Interface(input, output, completer, terminal) {
} }
if (typeof historySize !== 'number' || if (typeof historySize !== 'number' ||
isNaN(historySize) || Number.isNaN(historySize) ||
historySize < 0) { historySize < 0) {
throw new errors.RangeError( throw new errors.RangeError(
'ERR_INVALID_OPT_VALUE', 'ERR_INVALID_OPT_VALUE',

View File

@ -575,7 +575,7 @@ function REPLServer(prompt,
// display next prompt and return. // display next prompt and return.
if (trimmedCmd) { if (trimmedCmd) {
if (trimmedCmd.charAt(0) === '.' && trimmedCmd.charAt(1) !== '.' && if (trimmedCmd.charAt(0) === '.' && trimmedCmd.charAt(1) !== '.' &&
isNaN(parseFloat(trimmedCmd))) { Number.isNaN(parseFloat(trimmedCmd))) {
const matches = trimmedCmd.match(/^\.([^\s]+)\s*(.*)$/); const matches = trimmedCmd.match(/^\.([^\s]+)\s*(.*)$/);
const keyword = matches && matches[1]; const keyword = matches && matches[1];
const rest = matches && matches[2]; const rest = matches && matches[2];

View File

@ -177,7 +177,7 @@ function Zlib(opts, mode) {
if (opts) { if (opts) {
chunkSize = opts.chunkSize; chunkSize = opts.chunkSize;
if (chunkSize !== undefined && chunkSize === chunkSize) { if (chunkSize !== undefined && !Number.isNaN(chunkSize)) {
if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize)) if (chunkSize < Z_MIN_CHUNK || !Number.isFinite(chunkSize))
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
'chunkSize', 'chunkSize',
@ -187,7 +187,7 @@ function Zlib(opts, mode) {
} }
flush = opts.flush; flush = opts.flush;
if (flush !== undefined && flush === flush) { if (flush !== undefined && !Number.isNaN(flush)) {
if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush)) if (flush < Z_NO_FLUSH || flush > Z_BLOCK || !Number.isFinite(flush))
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'flush', flush); throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'flush', flush);
} else { } else {
@ -195,7 +195,7 @@ function Zlib(opts, mode) {
} }
finishFlush = opts.finishFlush; finishFlush = opts.finishFlush;
if (finishFlush !== undefined && finishFlush === finishFlush) { if (finishFlush !== undefined && !Number.isNaN(finishFlush)) {
if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK || if (finishFlush < Z_NO_FLUSH || finishFlush > Z_BLOCK ||
!Number.isFinite(finishFlush)) { !Number.isFinite(finishFlush)) {
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@ -207,7 +207,7 @@ function Zlib(opts, mode) {
} }
windowBits = opts.windowBits; windowBits = opts.windowBits;
if (windowBits !== undefined && windowBits === windowBits) { if (windowBits !== undefined && !Number.isNaN(windowBits)) {
if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS || if (windowBits < Z_MIN_WINDOWBITS || windowBits > Z_MAX_WINDOWBITS ||
!Number.isFinite(windowBits)) { !Number.isFinite(windowBits)) {
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@ -219,7 +219,7 @@ function Zlib(opts, mode) {
} }
level = opts.level; level = opts.level;
if (level !== undefined && level === level) { if (level !== undefined && !Number.isNaN(level)) {
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL || if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL ||
!Number.isFinite(level)) { !Number.isFinite(level)) {
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@ -230,7 +230,7 @@ function Zlib(opts, mode) {
} }
memLevel = opts.memLevel; memLevel = opts.memLevel;
if (memLevel !== undefined && memLevel === memLevel) { if (memLevel !== undefined && !Number.isNaN(memLevel)) {
if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL || if (memLevel < Z_MIN_MEMLEVEL || memLevel > Z_MAX_MEMLEVEL ||
!Number.isFinite(memLevel)) { !Number.isFinite(memLevel)) {
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', throw new errors.RangeError('ERR_INVALID_OPT_VALUE',
@ -241,7 +241,7 @@ function Zlib(opts, mode) {
} }
strategy = opts.strategy; strategy = opts.strategy;
if (strategy !== undefined && strategy === strategy) { if (strategy !== undefined && !Number.isNaN(strategy)) {
if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED || if (strategy < Z_DEFAULT_STRATEGY || strategy > Z_FIXED ||
!Number.isFinite(strategy)) { !Number.isFinite(strategy)) {
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', throw new errors.TypeError('ERR_INVALID_OPT_VALUE',