Skip to content

Commit 7051fe6

Browse files
cesco69Uzlopak
andauthored
perf: reduce if statement for NaN (micro-optimization) (#697)
* chore(perf): reduce if statement for `NaN` Signed-off-by: francesco <[email protected]> * isInteger return false for Infinity and NaN Signed-off-by: francesco <[email protected]> * fast check for NaN Signed-off-by: francesco <[email protected]> * disable lint Signed-off-by: francesco <[email protected]> * disable only for self-compare NaN Co-authored-by: Aras Abbasi <[email protected]> Signed-off-by: francesco <[email protected]> * fast check for NaN Signed-off-by: francesco <[email protected]> --------- Signed-off-by: francesco <[email protected]> Co-authored-by: Aras Abbasi <[email protected]>
1 parent b07e607 commit 7051fe6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/serializer.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ module.exports = class Serializer {
2525

2626
asInteger (i) {
2727
if (typeof i === 'number') {
28-
if (i === Infinity || i === -Infinity) {
29-
throw new Error(`The value "${i}" cannot be converted to an integer.`)
30-
}
3128
if (Number.isInteger(i)) {
3229
return '' + i
3330
}
34-
if (Number.isNaN(i)) {
31+
// check if number is Infinity or NaN
32+
// eslint-disable-next-line no-self-compare
33+
if (i === Infinity || i === -Infinity || i !== i) {
3534
throw new Error(`The value "${i}" cannot be converted to an integer.`)
3635
}
3736
return this.parseInteger(i)
@@ -52,7 +51,9 @@ module.exports = class Serializer {
5251

5352
asNumber (i) {
5453
const num = Number(i)
55-
if (Number.isNaN(num)) {
54+
// check if number is NaN
55+
// eslint-disable-next-line no-self-compare
56+
if (num !== num) {
5657
throw new Error(`The value "${i}" cannot be converted to a number.`)
5758
} else if (!Number.isFinite(num)) {
5859
return 'null'

0 commit comments

Comments
 (0)