← Back to context

Comment by hardwaregeek

4 years ago

Floating point is really really hard to get right, especially if you want the numbers to be stable. Which begs the question, why the heck does JavaScript, the most used language in the world, not have an integer type? Sure, there's BigInt but that's quite clunky to use. I know it's virtually impossible to add by now, but I'd love a integer type for all my bit twiddling, byte munching needs.

I just feel if you have bit twiddling, byte munching needs JavaScript shouldn't be the language of choice. Doing that is a rather rare edge case and if you're doing it for performance reasons, working in Javascript is the much bigger performance problem.

  • If you're already using JavaScript for some other reasons but occasionally have bit twiddling, byte munching needs, then trying to do that in JavaScript makes perfect sense. Is it the fastest option? No. But according to https://benchmarksgame-team.pages.debian.net/benchmarksgame/... it is generally within a factor of 4-5 of C++.

    For an application area where this applies, consider a web-based game. Using JavaScript keeps you from shipping another application. But occasionally you may have bit twiddling and/or byte munching needs. Which you need to do in JavaScript.

    • My point wasn't to say you should never do byte manipulation in Javascript, but that for these rare edge cases the existing capabilities without a native int are fine and if you would really need the native int rather than some equivalent work around (which I assume is most likely due to performance considerations) a faster language is the better approach. A webgame might be able to use wasm for that.

  • That may be true but another case where floating point should be avoided is with money. Now think about all the times a web developer innocuously used a JS number to represent a price. I wouldn’t be surprised if floating point errors affected billions of dollars of transactions.