Live data from Hacker News

BigInt Shipping in Firefox

wingolog.org

11–20 of 172 posts

Re: BigInt Shipping in Firefox

#11
post #5

Anyone knows how does bigint serialization and deserialization to JSON look like in practice in Chrome and Firefox?

JSON serialization does not seem to be supported yet. `JSON.serialize(42n)` gives me a TypeError "Do not know how to serialize a BigInt" in Chrome. Deserialization support also doesn't exist as far as I'm aware. Would be great to be able to choose to decode integer JSON literals as BigInt, but I'm not holding my breath.

Re: BigInt Shipping in Firefox

#14

I'm curious why this wasn't shipped as a replacement of the existing Number type implementation, instead of this new type and syntax.

Because Numbers in javascripts are floats, and not allowing floats in numbers would be a serious backward compatibility issue.

Also, I'm assuming there are performance issues with using bigints.

Re: BigInt Shipping in Firefox

#15

I'm curious why this wasn't shipped as a replacement of the existing Number type implementation, instead of this new type and syntax.

If I understand you correctly (which I’m not sure I do), I imagine it is because they can’t simply convert everyone’s floats ( type number) to ints (type bigint) as that would break a large portion of the internet.

Re: BigInt Shipping in Firefox

#17

I'm curious why this wasn't shipped as a replacement of the existing Number type implementation, instead of this new type and syntax.

Because Numbers in javascripts are floats, and not allowing floats in numbers would be a serious backward compatibility issue. Also, I'm assuming there are performance issues with using bigints.

Sounds like you're correct according to MDN:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re: BigInt Shipping in Firefox

#18
post #7

I 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 Uint8Array, and they were, ahem, “interesting.” I think a popular one built a byte-array abstraction on top of hex-encoded strings, and then built bignums on that.)

Actually supporting the literal syntax or the operators via a plain library is impossible; but you can just do it the other way around: encourage developers to use a bignum library (for now), and have said library just “bake down” to native BigInts when they’re available.

Since everybody until now who was using bignums in JS was using them through a library, this won’t be a hard change to make.

(And it’s already been done! https://www.npmjs.com/package/big-integer now bakes down to native BigInts when it can.)

As for the brazen developers who start writing code to directly use the native BigInt... I suppose they’ll have to have two versions of their compiled code units in their minified blob, with a loader shim that evals out the version of the class/module that does/doesn’t use bigints. Maybe we’ll see an JS-pipeline pass to generate this. (I’m not a JS dev, so I’m not sure if there’s already support for this kind of thing.)

Re: BigInt Shipping in Firefox

#19
Here is the proposal[0].

There are important real use cases for this. I authored an implementation of the FNV64 hash function[1] at my last job. I needed to use the BigInteger.js[2] library. It truly surprised me that Javascript in 2018 did not support something so simple as 64-bit integers.

[0]: https://tc39.github.io/proposal-bigint/

[1]: https://golang.org/src/hash/fnv/fnv.go

[2]: https://github.com/peterolson/BigInteger.js

Re: BigInt Shipping in Firefox

#20
post #18
post #7

I 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…

> Actually supporting the literal syntax or the operators via a plain library is impossible; but you can just do it the other way around: encourage developers to use a bignum library (for now), and have said library just “bake down” to native BigInts when they’re available.

Can't you rewrite the syntax into something usable at runtime? Not that it were a good idea.

Post reply on HN