JavaScript's BigInt in WebAssembly!

Background

JavaScript’s Number type is a float64 and is limited to 53 bits of actual value (as specified by IEEE 754).

Wasm offers a 64 bits integer type, there’s currently no efficient way to represent this type in JavaScript, which can be limiting.

JavaScript recently introduced the BigInt object, allowing the representation of arbitrary length integers.

Performance

The BigInt interpretation inevitably introduce an overhead, however the conversions happen only at the JavaScript boundaries. The i64 is then used directly in Wasm, which has no overhead.

Under the hood - v8

You can see the CL I made here: https://chromium-review.googlesource.com/c/v8/v8/+/1355144.

Demo

Note that, currently it's only supported when:

A SpiderMonkey (Firefox) is also work-in-progress.

Returning a 64 bits integer from WebAssembly

WebAssembly
        
(module
  (func export 'fn') (result i64)
    (i64.const 66)
  )
)
        
      

Result

exports.fn().constructor ===
exports.fn() ===

Passing from JavaScript and returning from WebAssembly a 64 bits integer

WebAssembly
        
(module
  (func (export 'fn') (param i64) (result i64)
    (get_local 0)
  )
)
        
      

Result

exports.fn(0n) ===
exports.fn(-0n) ===
exports.fn(123n) ===
exports.fn(-123n) ===

Reach out

Say hello: [email protected].

Ping me on Twitter: @svensauleau.