Down to the last bit: stdlib across engines
DEV Community

Down to the last bit: stdlib across engines

Can one library give one answer everywhere?

Can a library give the same answer on every JavaScript engine it runs on?

The previous post in this series, Whose maths is it?, showed why JavaScript's own Math can't give that: the specification leaves room. Of the 37 functions on Math in ECMAScript 2026, 14 are exact, with one permitted result for each input. Another 22 are implementation-approximated: the specification describes an ideal answer and leaves the bits to each engine. The 37th, Math.random, is a case of its own: its answers are meant to differ.

Engines fill that room differently. Some call the C library underneath them, some carry their own maths, and each weighs precision against speed for the work most JavaScript does.

So the same call can return different bits, depending on the engine, its version, and how it was built.

Where Math stops, developers reach for libraries: jStat for statistics; math.js for expressions, units, and matrices; and decimal.js for arbitrary precision.

None of that means JavaScript can't be used for numerical and scientific computing. The specification doesn't require its built-ins to be suited to it, but it does fix the arithmetic exactly, and a library can build on that. Languages people more readily reach for in numerical computing, like R or Python, have similar gaps without their packages, and they call down to C, C++, and Fortran to wrap libraries like BLAS and LAPACK. Node.js can do that too, through native addons.

What JavaScript has lacked is a shared standard library of numerical building blocks that packages can build on together, and that's where stdlib comes in.

Consistency across engines

Consistency across engines is one more job stdlib takes on, and it does so by bringing its own maths instead of borrowing the engine's. Its JavaScript special functions don't call Math.sin or Math.exp, so they never reach the engine's maths library, or the platform's C library behind it. They're built from what the specification fixes.

Down to the last bit, then: for the functions tested, on every engine they ran on, every number stdlib gave agreed.

The one difference was how engines stored "not a number" on a single test input, a difference JavaScript itself can't see.

One answer, wherever it runs

stdlib's runs show one answer, starting with a single value. Here's Math.tan(7) in three browsers on my Mac:

  • Chrome 0.8714479827243188
  • Firefox 0.8714479827243187
  • Safari 0.8714479827243189

Those are three answers that differ in the last digit, each from a different maths library, and all three allowed.

stdlib has its own tan, and in the same three browsers, it gives one answer:

  • Chrome 0.8714479827243187
  • Firefox 0.8714479827243187
  • Safari 0.8714479827243187

A single value is one data point; the broader evidence is two functions on thousands of inputs. The two are stdlib's error function, erf, and its ldexp, which scales a number by a power of two, and they ran on 4,053 test cases. Each result prints as the 64 bits of a double rather than as a decimal, so nothing is rounded away in the printing. Every line of output goes into one digest, a SHA-256 hash, so two runs with equal digests gave equal bits on every line. The code ran as a bundle: the published packages and everything they depend on, in one file, so even an engine that can't load a module by name can run it.

Chrome, Firefox, and Safari all gave stdlib's digest. Chrome matched, bit for bit, the digest that every other run is checked against, taken on Node.js. Firefox and Safari matched it on every line but one: a single input built to be a signalling NaN, one kind of "not a number," which they store in a pattern of their own, as the specification allows.

The same input, stored two ways: the signalling-NaN input's 64 bits, in hexadecimal. Source: this post's companion repository, the digest outputs and the browser records.

JavaScript can't tell one NaN from another: every NaN gives the same answer to isNaN, ===, and Object.is, and NaN === NaN is false, IEEE 754's rule, shared with C and Python, not a JavaScript quirk.

Only the raw bits, read through a typed array, which holds numbers as raw bytes, show that Firefox and Safari stored that input differently from Chrome. A double is 64 bits: a sign, 11 exponent bits, and 52 fraction bits, read here as two 32-bit words, high and low. A value is NaN when every exponent bit is set and the fraction isn't all zeros. NaNs come in two kinds, quiet and signalling, and the first fraction bit says which: 1 for quiet, 0 for signalling, which is why it's called the quiet bit.

The high word: the sign, the exponent, and the first 20 fraction bits. The low word is all fraction. Chrome kept every bit the test wrote, in both words, and Firefox and Safari stored zeros here. The low word: the last 32 fraction bits.

stdlib's erf and ldexp in three browsers, macOS arm64, 23 November 2026. In this table and the next two, "NaN-equal" means every line matches the digest's bits except the signalling-NaN input's, where only the stored bit pattern differs.

Browser Result Chrome 158.0.8127.37 bit for bit Firefox 160.0.1 NaN-equal Safari 27.2 NaN-equal

Node.js shows stdlib's digest holding while the engine's own Math moves. One release from each Node.js line, from 20 to the 27 alpha, ran stdlib's digest, and beside it, a test of Node.js's own Math functions. Node.js's Math gave the same results on Node.js 20 and 22, then changed with every release after, most on the 27 alpha, where even Math.tan(7) moves. Node.js's Math also differed between macOS arm64 and Linux x86_64 at every release. stdlib's digest held throughout, on both.

Node.js, one release per line, on macOS arm64 and Linux x86_64, 22 November 2026. Node.js's own Math is counted in lines of its 7,821-line test that differ; release-to-release counts are macOS arm64's. Release stdlib's digest, macOS arm64 / Linux x86_64 Node.js's Math, against the previous release Node.js's Math, macOS arm64 against Linux x86_64 20.20.2 bit for bit / bit for bit - 23 lines 22.23.1 bit for bit / bit for bit unchanged 23 lines 24.22.3 bit for bit / bit for bit 48 lines 23 lines 26.13.1 NaN-equal / NaN-equal 57 lines 23 lines 27.0.0-alpha.0.1.1 bit for bit / bit for bit 446 lines 27 lines

stdlib's digest came out the same well beyond browsers and Node.js. Engine shells and engines from the JavaScript engines zoo, a project that builds and catalogs JavaScript engines, ran the same bundle, each in its own shell. Other engines, each in its own shell, on Linux x86_64, Linux aarch64, and macOS arm64, October and November 2026. Engines Result V8 (d8), QuickJS, QuickJS-ng, GraalJS, Duktape, JerryScript, MicroQuickJS, Nova, NJS bit for bit SpiderMonkey, JavaScriptCore, XS, Hermes, engine262, Boa, LibJS NaN-equal Kiesel, goja, Rhino NaN-equal, through a change to how output is printed

In all, 19 engines ran stdlib's bundle, in browsers, in their own shells, and inside other runtimes, and every one gave stdlib's digest: bit for bit, or different only in the signalling-NaN input's bits.

Why stdlib's answers hold

The 22 implementation-approximated functions are where engines may differ, and stdlib's special functions don't call them.

stdlib's erf and ldexp are built from three kinds of part: arithmetic (+, -, *, and /), reading and writing typed arrays, and three of Math's exact functions, abs, ceil, and floor. erf needs an exponential, and it uses stdlib's own exp, not Math.exp.

ECMA-262 specifies every one of those parts exactly. Its arithmetic is IEEE 754's, rounded to the nearest double; abs, ceil, and floor are among Math's 14 exact functions; and reading or writing a typed array is fixed to the bit.

So on any engine whose arithmetic conforms, each input to erf or ldexp has one permitted result. That's a reading of the specification checked against stdlib's code, not something a run can prove; the runs show that the engines tested conform. erf is still an approximation, since no finite run of arithmetic gives it exactly. But the approximation is stdlib's, written out in its own code from operations whose results are fixed, so each answer is decided once, in stdlib, rather than by each engine it runs on.

Rhino, a JavaScript engine written in Java, shows the same thing from the engine's side. A development build of Rhino from before a fix gave Infinity for Math.asinh(1e300), a textbook formula overflowing. The 1.9.2 release, in October 2026, fixed it by borrowing V8's algorithms. Both builds give the same digest for erf and ldexp, because neither function calls Math.asinh.

stdlib's integer work agrees for the same reason. Three routes by which 8- and 16-bit typed arrays reach stdlib gave identical output on every engine that loaded them, as the specification says they must.

In JavaScript, stdlib's answers hold on one condition: the arithmetic underneath has to follow IEEE 754. In these runs, that condition failed once. An emulated 32-bit ARM build of MicroQuickJS did its floating-point arithmetic in software, and one subtraction inside erf rounded wrongly, so erf came out one last bit different on 15 of 5,944 inputs, taken from stdlib's own test fixtures for erf. The same subtraction, in plain C with no engine involved, misrounds the same way, and whether the software floating-point library or the emulator is at fault isn't established.

What stdlib needs to run

Every result so far assumes stdlib loaded first. stdlib is written to load with little more than the syntax of ES5, ECMAScript's 5th edition, and typed arrays. stdlib's code is written to avoid the syntax that arrived after ES5: arrow functions, classes, template literals, spread and rest, and generators. That's a house rule, not a limit of the language.

stdlib also needs typed arrays: arrays that hold one kind of number, such as Float64Array for doubles, as raw bytes in memory. Browsers shipped them in 2010 and 2011, from a specification that grew out of WebGL, the web's 3D graphics standard. ES2015 later wrote them into the standard, but engines of the ES5 era already had them.

Typed arrays let stdlib reach a number's bits, which JavaScript has no operator for. erf, for one, needs to square part of its input exactly: it keeps only the number's leading digits, whose square fits in a double with nothing rounded, and handles the rest as a small correction. The cheapest way to clear the trailing digits is two typed arrays over the same eight bytes, one that sees them as a double and one that sees them as two 32-bit integers.

Where an engine lacks a typed array or another built-in, stdlib's own checks reach for its fallback: its own version of the built-in, written in plain ES5. stdlib's Uint16Array, for example, is the engine's own wherever a check finds one, and a fallback everywhere else.

MicroQuickJS, an engine for embedded systems, always runs in its own stricter mode: strict mode plus a ban on array holes, direct eval, and boxed primitives such as new Number(1). stdlib's code is strict-mode code already. On MicroQuickJS, those checks carried stdlib past three missing or quirky built-ins: Symbol, Object.defineProperty, and Date.now.

The one exception among those checks is one that itself calls a built-in MicroQuickJS lacks, propertyIsEnumerable, in "Gaps that are stdlib's own." stdlib aims for ES5 syntax plus typed arrays, with stdlib's own fallbacks where they're missing. otto, an ES5 engine with no typed arrays, falls within that aim, and stdlib still stops there, because those fallbacks aren't written yet: the five that erf and ldexp need, for ArrayBuffer, Uint8Array, Uint16Array, Uint32Array, and Float64Array, are stubs that throw "not implemented." otto stops at the first one it reaches, Uint16Array's.

stdlib has written one real fallback for a typed array, for Float16Array, which ES2025 added and Node.js has only from version 24, and that fallback is the model for the rest.

Where stdlib doesn't run, and why

In each case here, as of this writing, stdlib doesn't give a different answer: it fails to load. Half of those failures come from other projects' choices or limits. Two come from gaps in stdlib's own code, and one from a deliberate choice of stdlib's meeting an engine's limit. Each is a mismatch between what stdlib's code assumes and what an engine provides.

Different subsets of the language otto and Elk each run a different subset of JavaScript, and the two subsets barely overlap. Elk, built for microcontrollers, runs a declared subset of ES2015; otto, written in Go, targets ES5.

Here are four separate one-line programs, and what each engine made of them:

  • var a = 1 ; console . log ( a ) ; // otto runs it; Elk: 'var' not implemented
  • let a = 1 ; console . log ( a ) ; // Elk runs it; otto: Unexpected identifier
  • const a = 1 ; console . log ( a ) // otto: Unexpected reserved word; Elk: 'const' not implemented
  • console . log ( 1 + 2 ) ; // both print 3

Neither engine runs const. So no program that declares a variable with var, let, or const runs on both, and one that declares nothing does. stdlib writes ES5, and Elk leaves out var on purpose, while stdlib declares everything with it. That's a mismatch of aims, not a limit on either side. Every engine in today's browsers and in Node.js runs ES5, though: JavaScript's standard doesn't drop what existing code relies on, even features it calls undesirable, and those engines keep to it, which is why a page written in the 1990s can still run in a browser today.

ES5 also reaches past those engines: all five stdlib packages tried ran unmodified from source on MicroQuickJS, and one bundle ran in every engine shell in the tables, none of which can load a module by name. otto is Elk's opposite: an ES5 engine with no typed arrays is one stdlib aims to run on. Where it stops is a gap of stdlib's own.

Engines that stop early Quanta, Porffor, and scriptc fail before any stdlib maths runs, each for its own reason, as measured at the versions

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.