Compare a Python implementation against JavaScript
Two implementations in different languages can be compared by running both against the same inputs and canonicalizing their results, so that differences of language alone, such as Python returning a tuple where JavaScript returns an array, do not read as behavioral divergence.
JavaScript, TypeScript and Python run in your browser. Nothing to install, no account.
Both sides run in your browser
JavaScript and TypeScript execute in a Web Worker; Python executes in Pyodide, CPython compiled to WebAssembly. The Pyodide runtime is served with the app rather than pulled from a CDN, so a Python-against-JavaScript comparison needs no install, no server and no network once the page has loaded.
Differences that come only from the language
Without explicit rules, a port gets flagged for differences that come from the languages themselves. Each of these is a toggle:
-
Noneagainstnull. The same idea with two spellings; almost always you want these treated as equal. -
Tuple against array. JavaScript has no tuple, so a Python
function returning
(a, b)can only ever be matched by an array. -
1against1.0. Python distinguishes integers from floats; JavaScript numbers are one type. If the port is meant to preserve integer arithmetic, turn the strict setting on. - Dictionary key order. Both languages keep insertion order for ordinary keys (JavaScript sorts integer-like keys first), but a port often builds a mapping in a different order while meaning the same mapping.
- Set order. With strict set order off, sets compare as multisets; ambiguous pairings fail toward divergence rather than being optimistically matched.
- Float tolerance. An epsilon, because the two languages may disagree in the last bits of a long floating-point computation, and requiring an exact match then tests floating-point rounding instead of your port.
Where ports actually diverge
The same handful of differences show up repeatedly in ports:
-
Integer division. Python's
//floors, so-7 // 2is-4. JavaScript'sMath.trunc(-7 / 2)is-3. Positive inputs agree, so hand-written tests tend to pass and generated inputs find the difference. -
Modulo of negatives.
-7 % 3is2in Python and-1in JavaScript. -
Sorting. JavaScript's default
sortcompares stringified values, so[9, 10]sorts to[10, 9]. -
Rounding. Python's
roundis banker's rounding:round(0.5)is0, whileMath.round(0.5)is1. - Errors. Error messages are compared exactly at every fuzziness setting, because two different exceptions usually share most of their text, and a threshold loose enough to match them would call unrelated failures the same behavior.
Questions
Can I check whether a Python function and a JavaScript function return the same thing?
Yes. Both run in your browser (JavaScript in a Web Worker and Python in Pyodide) against the same generated inputs, and their results are compared after canonicalizing differences that are purely differences of language.
How are Python and JavaScript values compared when the types do not line up?
Through explicit toggles for each mismatch: None against null, tuple against array, int against float, dictionary key order, set order, and a float tolerance. You decide which of those count as real differences for your comparison.
What usually differs between a Python function and its JavaScript port?
Integer division and modulo on negative numbers, the default sort comparing values as strings in JavaScript, and banker’s rounding in Python. They tend to agree on typical hand-picked inputs and can disagree on generated ones.
Do I need to install Python?
No. Python runs through Pyodide, which is CPython compiled to WebAssembly and served with the app rather than from a CDN, so it works in the browser and keeps working offline.