From 7caca286623f06be33ae3a1c2a38e6f974aa9276 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 13 Mar 2024 16:07:25 +0100 Subject: [PATCH 1/6] chore(perf): reduce if statement for `NaN` Signed-off-by: francesco --- lib/serializer.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index 387e8927..a320fe2c 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -25,15 +25,12 @@ module.exports = class Serializer { asInteger (i) { if (typeof i === 'number') { - if (i === Infinity || i === -Infinity) { + if (i === Infinity || i === -Infinity || Number.isNaN(i)) { throw new Error(`The value "${i}" cannot be converted to an integer.`) } if (Number.isInteger(i)) { return '' + i } - if (Number.isNaN(i)) { - throw new Error(`The value "${i}" cannot be converted to an integer.`) - } return this.parseInteger(i) } else if (i === null) { return '0' From 3bea52073f1fccd83885a1513cf9e471d6276210 Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 14 Mar 2024 11:28:01 +0100 Subject: [PATCH 2/6] isInteger return false for Infinity and NaN Signed-off-by: francesco --- lib/serializer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index a320fe2c..c6a0999b 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -25,12 +25,12 @@ module.exports = class Serializer { asInteger (i) { if (typeof i === 'number') { - if (i === Infinity || i === -Infinity || Number.isNaN(i)) { - throw new Error(`The value "${i}" cannot be converted to an integer.`) - } if (Number.isInteger(i)) { return '' + i } + if (i === Infinity || i === -Infinity || Number.isNaN(i)) { + throw new Error(`The value "${i}" cannot be converted to an integer.`) + } return this.parseInteger(i) } else if (i === null) { return '0' From e14a18cc56743900578878073d1539ff241d36d0 Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 14 Mar 2024 13:49:01 +0100 Subject: [PATCH 3/6] fast check for NaN Signed-off-by: francesco --- lib/serializer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index c6a0999b..38ae3122 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -28,7 +28,8 @@ module.exports = class Serializer { if (Number.isInteger(i)) { return '' + i } - if (i === Infinity || i === -Infinity || Number.isNaN(i)) { + // check if number is Infinity or NaN + if (i === Infinity || i === -Infinity || i !== i) { throw new Error(`The value "${i}" cannot be converted to an integer.`) } return this.parseInteger(i) From 2e5521554ca8d9fc5099657b36ae54418f63e4f0 Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 14 Mar 2024 14:22:16 +0100 Subject: [PATCH 4/6] disable lint Signed-off-by: francesco --- lib/serializer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/serializer.js b/lib/serializer.js index 38ae3122..4a1360cf 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -29,6 +29,7 @@ module.exports = class Serializer { return '' + i } // check if number is Infinity or NaN + // eslint-disable-next-line if (i === Infinity || i === -Infinity || i !== i) { throw new Error(`The value "${i}" cannot be converted to an integer.`) } From 53fd6282f2f77bfb867aefd7a2a342a1a6a34827 Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 14 Mar 2024 14:31:15 +0100 Subject: [PATCH 5/6] disable only for self-compare NaN Co-authored-by: Aras Abbasi Signed-off-by: francesco --- lib/serializer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index 4a1360cf..83ec93e6 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -29,7 +29,7 @@ module.exports = class Serializer { return '' + i } // check if number is Infinity or NaN - // eslint-disable-next-line + // eslint-disable-next-line no-self-compare if (i === Infinity || i === -Infinity || i !== i) { throw new Error(`The value "${i}" cannot be converted to an integer.`) } From 54b35d1c04905c2e3ecfc6d915a72a356c537a5b Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 14 Mar 2024 14:46:38 +0100 Subject: [PATCH 6/6] fast check for NaN Signed-off-by: francesco --- lib/serializer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index 83ec93e6..c6865226 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -51,7 +51,9 @@ module.exports = class Serializer { asNumber (i) { const num = Number(i) - if (Number.isNaN(num)) { + // check if number is NaN + // eslint-disable-next-line no-self-compare + if (num !== num) { throw new Error(`The value "${i}" cannot be converted to a number.`) } else if (!Number.isFinite(num)) { return 'null'