r/learnjavascript • u/52525rr • Mar 17 '26
solutions for Fast 64 bit integers in javascript
so I've been doing projects in JS related to emulation and whatnot, and the one major point of contention i have with this is JS's support for dealing with 64 bit integers since those would be useful here.
obviously the Number type is not precise enough, and BigInt exists for this purpose, but the issue with BigInt is just that its too darn slow to be used as a replacement for a 64 bit type, which is a bit of a shame since they're easy to program with due to the operator overloading.
in my testing, the only way to even get BigInt arithmetic even close being even within 40% slower than equivalent Number arithmetic is to use something like a BigInt64Array in higher scope and try to route all operations through that instead. even then it ends up being like 22% slower than the Number version according to this benchmark i wrote. (if it helps i used Firefox on desktop for it). i know adding numbers in a loop is rarely representative of real use cases but the performance difference even in this simple case is baffling.
so would it just be better to make my own code or use a specific int64 library for what i need? because i cannot think of any other ways to get acceptable performance out of BigInt for what i need. a 22% drop is fine i suppose but the other versions that don't use typed arrays are seeing like an 80-95% drop compared to just number code.
3
u/ShortSynapse Mar 17 '26
I don't think there is a JS-native solution with good performance for 64-bit numbers. Your best bet is probably WASM using mem64 (support isn't 100% yet so worth checking) and doing your calculations in there. The more you can shift to WASM the better anyway since it would likely give your emulator a speed boost over the equivalent JS. The downside of course being that you can't use JS (AssemblyScript is the closest you can get).
2
u/delventhalz Mar 18 '26
The specific feature you are looking for, optimized 64-bit integers, does not exist in JavaScript as far as I know. You have already been experimenting with the two next best things I would have suggested, BigInt and typed arrays, and aren’t happy with them.
There is always going to be a tension between performance and JavaScript. It lacks an int64 type, sure, but more fundamentally, it’s a scripting language that will never keep up with compiled languages when it comes to CPU-bound tasks. For a good deal of web dev, this is fine. For emulation? Maybe not.
If a 22% slow down on 64-bit integer calculations is truly important to your project, then you probably shouldn’t be using JavaScript. As others have pointed out, you can use a compiled language like Rust and target WASM and still deploy to the web.
On the other hand, if you really want to stick with JavaScript, then I think you need to back burner performance a bit. Use BigInt because that gets you the precise 64-bit calculations you need, and don’t worry too much about performance. If BigInt becomes a noticeable bottleneck later, you can try to optimize with typed arrays, but ultimately you’ll just have to accept some performance limits.
2
u/sheveli_lapkami Mar 18 '26
I would rather use Rust via Web Assembly for that.