Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upFixing JSON decoding for Ints. #153
Conversation
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Oops, sorry - that should read |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
krisajenkins
May 22, 2015
Contributor
This bug has just bitten me again, which in turn has nudged me to rebase against the current master branch. :-)
|
This bug has just bitten me again, which in turn has nudged me to rebase against the current master branch. :-) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ajhager
Aug 1, 2015
Contributor
I have benchmarked and unit tested the original code, your proposed change, and a new method of my own design (original, proposed, and blend respectively below.) I used the tests from https://github.com/parshap/js-is-integer/blob/master/test.js and the benchmarks are at http://jsperf.com/isinteger-elm.
The original does indeed fail for values over and under 2147483647. proposed and blend are both correct, but blend is only slightly slower than the original in the general case and nearly equivalent in the typical case.
function original(value) {
return typeof value === 'number' && (value|0) === value
}
function proposed(value) {
return typeof value === "number" && isFinite(value) && value > -9007199254740992 && value < 9007199254740992 && Math.floor(value) === value;
}
function blend(value) {
if (typeof value !== 'number') {
return false;
}
if (value < 2147483647 && value > -2147483647) {
return (value|0) === value;
}
return isFinite(value) && !(value % 1);
} No matter which function we go with, there needs to be some JSON tests to cover this. I would be happy to add those though.
|
I have benchmarked and unit tested the original code, your proposed change, and a new method of my own design (original, proposed, and blend respectively below.) I used the tests from https://github.com/parshap/js-is-integer/blob/master/test.js and the benchmarks are at http://jsperf.com/isinteger-elm. The original does indeed fail for values over and under 2147483647. proposed and blend are both correct, but blend is only slightly slower than the original in the general case and nearly equivalent in the typical case. function original(value) {
return typeof value === 'number' && (value|0) === value
}
function proposed(value) {
return typeof value === "number" && isFinite(value) && value > -9007199254740992 && value < 9007199254740992 && Math.floor(value) === value;
}
function blend(value) {
if (typeof value !== 'number') {
return false;
}
if (value < 2147483647 && value > -2147483647) {
return (value|0) === value;
}
return isFinite(value) && !(value % 1);
} No matter which function we go with, there needs to be some JSON tests to cover this. I would be happy to add those though. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Aug 2, 2015
Member
@krisajenkins, thanks for providing a fix for this! I was not sure whether the behavior you expect is the behavior everyone would expect. If @ajhager agrees, I guess there is some sort of consensus :)
I'm trying this new thing out where I close issues more aggressively, even if they are a good idea that should happen. In this case, it seems like there are multiple folks who can do the updated fix, so to avoid a chance of blocking, can we do a new PR with the faster version and some tests?
|
@krisajenkins, thanks for providing a fix for this! I was not sure whether the behavior you expect is the behavior everyone would expect. If @ajhager agrees, I guess there is some sort of consensus :) I'm trying this new thing out where I close issues more aggressively, even if they are a good idea that should happen. In this case, it seems like there are multiple folks who can do the updated fix, so to avoid a chance of blocking, can we do a new PR with the faster version and some tests? |
evancz
closed this
Aug 2, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
I will do the new PR with tests. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Aug 2, 2015
Member
Awesome, thank you!
Also, sorry for the huge delay @krisajenkins, and thank you again @ajhager for unsticking things! :)
|
Awesome, thank you! Also, sorry for the huge delay @krisajenkins, and thank you again @ajhager for unsticking things! :) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
krisajenkins
Aug 2, 2015
Contributor
Cool, this'll be great to see. I'm decoding some JSON with some large productIDs, and having to manually meddle with elm-stuff keeps me awake at night. :-D
|
Cool, this'll be great to see. I'm decoding some JSON with some large |
krisajenkins commentedFeb 2, 2015
One way to check if a number is an integer in JavaScript is (value|0).
Sadly, this fails if the value is greater than 2^32, but still less than
Number.MAX_SAFE_INT.
This patch switches the Json decoder to use a polyfill of the proposed
ES6 Number.isInteger function.