Skip to content

Commit 07a9bcc

Browse files
authored
improve performance of asNumber (#595)
1 parent c650966 commit 07a9bcc

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/serializer.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@ module.exports = class Serializer {
5050
}
5151

5252
asNumber (i) {
53-
const num = Number(i)
54-
if (Number.isNaN(num)) {
53+
if (typeof i !== 'number') {
54+
i = Number(i)
55+
}
56+
// NaN !== NaN
57+
if (i !== i) { // eslint-disable-line no-self-compare
5558
throw new Error(`The value "${i}" cannot be converted to a number.`)
56-
} else if (!Number.isFinite(num)) {
57-
return null
58-
} else {
59-
return '' + num
6059
}
60+
if (i === Infinity || i === -Infinity) {
61+
return 'null'
62+
}
63+
return '' + i
6164
}
6265

6366
asBoolean (bool) {

0 commit comments

Comments
 (0)