15 ms·
BigInt Shipping in Firefox
- 1f60c 8y agoThis is big news! Shouldn’t the title be amended to include that it’s in Firefox Beta for now?
- cpeterso 8y agoBitInt's "n" suffix appears to stand for "numeric", but that seems confusing when typeof 1 === "number" and typeof 1n === "bigint". Why not an "i" suffix for "integer"? I understand that a "b" suffix is probably reserved in case JavaScript adds support for binary literals. https://tc39.github.io/proposal-bigint/#prod-BigIntLiteralSuffix https://tc39.github.io/proposal-bigint/#prod-BigIntLiteralSu...
- loeg 8y ago1i is a bit more difficult to read than 1n in many fonts. I don't think any numeral is plausibly confused with n, so 'iNt' makes some sense as a suffix IMO.
- GordonS 8y agoAs an aside, c# uses "L" to denote literals as 64-bit integers. "l" works too, but "L" is encouraged for the same reason you give ("l" could be confused as "1")
- matthewbauer 8y agoMaybe to avoid confusion with mathematical imaginary notation.
- ash_gti 8y ago'i' suffix are often used in languages for complex numbers. For example, in ruby '1i' evaluates to 0+1i as a complex number. I suspect they didn't want to conflate the two types.
- GordonS 8y ago64-bit integers are referred to as 'long' in languages I'm more familiar with, so personally I would have expected an "L" suffix. 32-bit integers are referred to as 'integers' in many languages, so I wouldn't expect an 'i' suffix. In any case, I certainly agree that "n" is confusing since Javascript already has a numeric type.
- vanderZwan 8y agoI suspect it is more that in mathematics, n is used to indicate integer values as opposed to for example x, which is used with real values.
- SomeOldThrow 8y ago> BitInt's "n" suffix appears to stand for "numeric" It actually stands for "not numeric"
- snek 8y agoI'm so glad we have two shipping implementations now
- deleted 8y ago[deleted]
- warpech 8y agoAnyone knows how does bigint serialization and deserialization to JSON look like in practice in Chrome and Firefox?
- wpq0 8y agoAfaict, JSON serialization is not supported yet, got a "TypeError: BigInt value can't be serialized in JSON". I'm on Firefox Developer edition 68.0b3.
- M2Ys4U 8y agoI suppose one could use a replacer[0] when calling JSON.stringify and a reviver[1] when calling JSON.parse [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- bpye 8y agoYou can certainly serialize to a string but knowing when you should revive to a BigInt seems tricky. You could prefix the strings I guess?
- tzs 8y agoHow about serializing to an array ["bigint", "170141183460469231731687303715884105727"]? It takes more space, but is easy to recognize, and easy to generalize to other types.
- jfk13 8y agoPresumably that could be confusing if you ever happen to have an actual array whose first element is the string "bigint".
- deleted 8y ago[deleted]
- philbarr 8y agoI feel sorry for whoever has to write the polyfill for this...
- acdha 8y agoYeah, the Chrome team basically recommended not to, favoring using something like Babel instead: https://developers.google.com/web/updates/2018/05/bigint#polyfilling_and_transpiling_bigints https://developers.google.com/web/updates/2018/05/bigint#pol...
- anoncake 8y ago> and they are also making it infeasible (in most cases) to transpile BigInt code to fallback code using Babel or similar tools. That doesn't sound like a recommendation.
- tzs 8y agoYeah. The recommendation is to write your code now making explicit calls to a specific bigint library. Later, when the JavaScript in the browsers you need to target has built in bigint support, Babel can replace your library calls with code that uses the built in bigints. For this to work, you need a library whose bigint behavior either exactly matches the built in implementation, or whose deviations are known and can reasonably be account for when translating to built in bigints. The library they recommend is a JavaScript translation of their implementation of built in bigints, so should match numerically what the built in implementation does. There may be other libraries that would be suitable, but it would be nice if everyone agreed on just one for this so that when the time comes to use Babel or similar to replace the library calls with use of the built ins, it only has to deal with one library.
- acdha 8y agoNote the comma: I was specifically referring to the advice against attempting to polyfill it. The Babel reference was attempting to convey the idea that instead you need something on the same order of complexity as a compiler to do this properly, but the details about JSBI are why I linked to their writeup rather than trying to summarize it.
- rockclimber 8y agoFirefox, please bring back SharedArrayBuffer like Chrome did so my JavaScript GTA will work again http://bad.city http://bad.city . I already removed juggling of transferable objects and SAB is so much easier to work with(less code and better performance too)
- andreigheorghe 8y agoI'm curious why this wasn't shipped as a replacement of the existing Number type implementation, instead of this new type and syntax.
- Illniyar 8y agoBecause 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.
- 52-6F-62 8y agoSounds like you're correct according to MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Cryptography https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- buzzerbetrayed 8y agoIf 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.
- mschuetz 8y agoBecause 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.
- lucb1e 8y agoPython has only Number though, and that can get as large as fits in your memory. Not sure why they didn't ship this with decimals and enabled it by default in javascript. 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.
- mourner 8y agoThat's awesome! Works great on my Pi digits calculation demo :) https://observablehq.com/@mourner/calculating-pi-digits https://observablehq.com/@mourner/calculating-pi-digits
- vngzs 8y agoHere 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/ https://tc39.github.io/proposal-bigint/ [1]: https://golang.org/src/hash/fnv/fnv.go https://golang.org/src/hash/fnv/fnv.go [2]: https://github.com/peterolson/BigInteger.js https://github.com/peterolson/BigInteger.js
- wahern 8y agoJavaScript can't easily evolve past the optimization tricks used in the major implementations. They all heavily rely on NaN tagging which makes 64-bit value types problematic. For similar reasons the architecture of V8 in particular has effectively dictated the design of WebAssembly, especially regarding control flow constructs like goto and coroutines.
- cwzwarich 8y ago> For similar reasons the architecture of V8 in particular has effectively dictated the design of WebAssembly, especially regarding control flow constructs like goto and coroutines. The weirdness of WebAssembly with regards to control flow constructs is rooted in Emscripten's "relooper", which was developed in a naive manner ignoring previous (superior) research on irreducible control flow.
- kevingadd 8y agoYes, but in both cases this is because the JS runtimes (v8 in particular) makes it really difficult to feel okay with unconstrained control flow. The relooper may have been an influence but it was primarily pressure from the v8 people (I was in these meetings) I'm less certain on my memory but I recall that Spidermonkey was more easily able to do fancier things with control flow.
- cwzwarich 8y ago
- deathanatos 8y ago> This contrasts with JavaScript number values, which have the well-known property of only being able to precisely represent integers between -2⁵³ and 2⁵³. JS's Number can represent way more integers than just those between -2⁵³ and 2⁵³. There's a great StackOverflow answer[1] that answers a similar question, ("What is the biggest "no-floating" integer that can be stored in an IEEE 754 double type without losing precision?") which also applies here (A JS Number is an IEEE double): > The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX or approximately 1.8 × 10³⁰⁸ (if your double is an IEEE 754 64-bit double). It's an integer. It's represented exactly. What more do you want? > Go on, ask me what the largest integer is, such that it and all smaller integers can be stored in IEEE 64-bit doubles without losing precision. An IEEE 64-bit double has 52 bits of mantissa, so I think it's 2⁵³: More seriously, this is great news, and I think it'll be nice to have such a numeric type in JS. [1]: https://stackoverflow.com/a/1848762 https://stackoverflow.com/a/1848762
- deleted 8y ago[deleted]
- taeric 8y agoYou seem to be doing an uncharitable reading of what you quoted. They can precisely represent numbers outside that range, true. They cannot represent all numbers outside that range.
- chrisseaton 8y agoThat's obviously not what was intended by the question, come on.
- jshowa3 8y agoWhat's with the comment at the end of the blog entry? lol.
- azinman2 8y agoI know right? Big accusation (that’s wrong), with nothing to back it up?
- alxmdev 8y agoMaybe it's the algorithmic evolution of those gibberish comments WordPress blogs used to get all the time...
- anchpop 8y agoI think the comment has been deleted, do you remember what it was?
- jshowa3 8y agoBasically, somebody left a comment saying the author was wrong and didn't know what he was talking about in a very aggressive way without demonstrating where it was wrong and why. Sorry for the drama. I know its off topic, but its all I can contribute since I know very little about the implementation of base data types.
- 3JPLW 8y agoAmusingly the drama continued with a new post: > censoring my comment will not resolve the inherent dishonesty of this article!!! Even more amusingly, the commenter's name links to disney.com. Or perhaps it is a strange new astroturfing campaign.
- timerol 8y agoKeeping up: The commenter has now acknowledged that the previous accusation was baseless
- dlbucci 8y agoKind 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 numbers or bigints. In that case, it's actually probably good that you can easily tell the difference using typeof, without requiring some hacky fix like `Array.isArray`. It does mean that "don't mix numbers and bigints" is going to become very popular JS advice soon.
- 1f60c 8y agoWhat makes you think that, exactly?
- metalliqaz 8y agoIt seems to me that in applications where there is widespread use bigint, you would simply be doing `typeof a === 'bigint'`. And, in nearly all other applications, it would remain as simply `typeof a === 'number'`
- sbr464 8y agoThat’s what we did for our financial and technical (science/math) backend services that use node.js. We just don’t use the built-in number/float libraries, instead using an abstraction library based on bignumber.js/decimal.js etc, consistently.
- jchw 8y agoTo be fair, it really shouldn’t be conflated with ‘number’ - it isn’t the same thing, the behavior differs. And you can (and probably should) use small helper functions for these kinds of checks, pretty much for this kind of flexibility. Kind of curious what other libs (like lodash) will do.
- wvenable 8y agoIf you're really going to do it right, that small function should also be published as npm package (isnumeric) that takes at least 2 other packages (isnumber and isbigint) as dependencies.
- stcredzero 8y agoNow I can have 2^128 grid squares in my procedurally generated browser MMO, instead of 2^106. That sounds like a joke, but it's actually a fact.
- Lowkeyloki 8y agoI can't wait to kick the tires on this feature. I wonder how it compares to the integer implementation in WebAssembly.
- recursive 8y agoWebAssembly has not arbitrary precision integers, so presumably is able to execute arithmetic as direct hardware instructions. bigint, on the other hand, is arbitrary precision, so it obviously can't do that.
- crazygringo 8y agoFor context, more general browser compatibility: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... In Chrome, but not Safari or Edge.
- CharlesW 8y agoFor those who care about JavaScript in embedded environments, Moddable released BigInt support in their open-source XS engine a few months ago: http://blog.moddable.com/blog/bigint/ http://blog.moddable.com/blog/bigint/
- krispbyte 8y agoOnce I was working with a database with large integer ids loading them through json and showing them in the browser. There was a bug where ids didn't match when updating a record. The issue was that the json decoder for Javascript didn't handle large numbers, so an id like 12345678901234567 would show up as 12345678901234568. That was fun to debug. I wonder if the interpreters will convert between BigInts and small ints automatically now? The backend side was PHP and it did so we never noticed the problem there.
- tynorf 8y agoThis is one reason why people use strings for IDs in JSON, even if they are actually integers.
- ggm 8y agocan it represent and manipulate ipv6 addresses (128 bit quantities) efficiently? not having to split into two 64 bits or deal with them as bit-strings may be an improvement for some things
- dfox 8y agoInternally it is implemented by spliting the number into an array of conveniently sized chunks (traditionally called limbs). That might mean either the machine word size for optimized assembly implementation on many common platforms or one bit less than native word size for portable C implementation (and architectures without carry flag) or some fixed number like 15 bits (IIRC mbedTLS has or at least used to have hardcoded limb size of 15bits in its bignum implementation).
- qwerty456127 8y agoNow let's wait another decade before they add BigDecimal...
- Gibbon1 8y agoI think a friend of mine said he was in a meeting back when Javascript was being standardized. Mike Cowlishaw proposed that Javascript use REXX's Decimal type instead of IEEE 754. Everyone thought that was a really bad stupid idea.
- qwerty456127 7y agoIt probably is when it's about "instead". IEEE 754 floating point computations are much much faster (given hardware support) while the precision is sufficient for most of the applications that don't have to deal with money.
- Gibbon1 7y agoI talked to my friend, he said Mike Cowlishaw tried very patiently explaining multiple times the need for a decimal type and basically ran into your argument. IE. If we provide a decimal type as a default. Then everyone will use it and it'll be 'slow'. Thus this is a bad idea. So we should not make that even an option. Because IEEE 754 is fast and fast is 'good' Frankly that decision was the worst language decision ever made bar none.
- frutiger 8y agoWhile not directly related to BigDecimal, my employer (Bloomberg) primarily sponsored this effort through the arduous TC39 process and sponsored the implementation in any browser vendor that wanted primarily as a stepping stone to standardising decimal floating point. We see BigInt as a relatively uncontroversial but important extension of the numeric type system of JavaScript. Once the door is open for a type that is not immediately compatible with Number, potentially new numeric types can be introduced. Perhaps BigDecimal (or Rational?) can follow.
- bmn__ 7y ago
- deleted 7y ago[deleted]