Earlier quoted context omitted.
You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get() { "use strict"; return () => String(this); } }); Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might f…
Only in JS do people seem okay with constantly mucking with built-ins.
BigInt Shipping in Firefox
61–70 of 172 posts
Re: BigInt Shipping in Firefox
#62I feel sorry for whoever has to write the polyfill for this...
Re: BigInt Shipping in Firefox
#63I feel sorry for whoever has to write the polyfill for this...
Bignums themselves are actually pretty easy to implement as a plain library, provided your language has a uint8-array type (not just a String type, which might require the content be utf-8 valid, or might only have facilities to operate on codepoints, or might cut the data off at the first NUL.) JS already has a Uint8Array, so bignum libraries are easy. (Let me tell you though, there were JS bignum libraries before U…
Re: BigInt Shipping in Firefox
#64Earlier quoted context omitted.
I'm sure its for backwards compatibility but hot damn is that one hell of a footgun. Even with that in place would you ever feel confident returning an id that didn't fit in a double knowing the sheer amount of code that definitely didn't handle that case?
Only Strings are safe, forty years past K&R. Wish I knew Tcl.
But stay away from TCL...
It is such a mess.
Re: BigInt Shipping in Firefox
#65Earlier quoted context omitted.
You can define a toJSON property on the BigInt prototype that gets used for serialization: Object.defineProperty(BigInt.prototype, "toJSON", { get() { "use strict"; return () => String(this); } }); Of course you could also serialize to a Number, but that risks losing precision. For example, the Twitter API often returns IDs as both a Number ("id") and a String ("id_str") to safely handle cases where the value might f…
I'm sure its for backwards compatibility but hot damn is that one hell of a footgun. Even with that in place would you ever feel confident returning an id that didn't fit in a double knowing the sheer amount of code that definitely didn't handle that case?
Re: BigInt Shipping in Firefox
#66I feel sorry for whoever has to write the polyfill for this...
[1]: https://github.com/GoogleChromeLabs/jsbi
[2]: https://github.com/GoogleChromeLabs/babel-plugin-transform-j...
Re: BigInt Shipping in Firefox
#67I'm curious why this wasn't shipped as a replacement of the existing Number type implementation, instead of this new type and syntax.
Because number is practically the same as double, and you can't just go and change double values to integer values. Also, doubles are fast. BigInts are not.
I once wrote a replacement for the + operator in javascript because computers can't do proper addition (0.1+0.2!=0.3). It was basically remembering the sign and handling some other notation like 1e100, splitting on the dot, and adding them up the normal way (as integers). To merge the result of the two additions, take care of the carry (if any) and concatenate the two numbers again with a dot.
So from my primitive understanding, if you're already storing a variable-length number (a space win in most cases, compared to the previous 4-byte float), you might as well store two variable-length numbers. Something like ABxx xxxx for the first byte, where if A=0 there is no decimal part (no second variable-length number) and if B=0 there is no next byte (the variable length part of it), then use Bxxx xxxx for each following byte. Then you have arbitrary size and decimals with perfect addition (no floating point approximations anymore).
Would it be that much slower, that it's an awful idea to do by default? Like, would it be noticeable on an average website with the usual godawful amount of javascript? Python gets away with it, so that would be weird. And you could still introduce special syntax (like, I don't know, suffixing an n maybe) to use old and faster primitive types for those who really need that.
Re: BigInt Shipping in Firefox
#68Earlier quoted context omitted.
You can certainly serialize to a string but knowing when you should revive to a BigInt seems tricky. You could prefix the strings I guess?
How about serializing to an array ["bigint", "170141183460469231731687303715884105727"]? It takes more space, but is easy to recognize, and easy to generalize to other types.
Re: BigInt Shipping in Firefox
#69Earlier quoted context omitted.
I’m super strongly against this concept actually, which you might gather from mention of lodash.
I think they were making a joke.
Re: BigInt Shipping in Firefox
#70Kind of bummed this is a new primitive type. Every instance of `typeof a === 'number'` just became `typeof a === `number` || typeof a === 'bigint'`... EDIT: The more I think about it (and the more comments I get), the more I think this might actually be a good thing. I think bigint will probably not be used in places where integer numbers are currently used, and it might be unreasonable to expect code to work with nu…