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. Some are settled for you and the rest are toggles:
-
Noneagainstnull. The same idea with two spellings, so they always match. Python has noundefined, soNonematches that too, and a function with no return value matches one on the other side. Between two JavaScript implementations a toggle separatesundefined, often a missingreturn, fromnull. -
Tuple against array. JavaScript has no tuple, so a Python
function returning
(a, b)always matches an array of the same items. The strict setting separates a tuple from a list only when both sides are Python. -
1against1.0. Python distinguishes integers from floats; JavaScript numbers are one type, so a JavaScript2matches Python's2.0and this comparison cannot tell you whether a port preserved integer arithmetic. Between two Python implementations the strict setting does: on,4 / 2and4 // 2differ. -
Dictionary key order. Not compared. 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, and Python's own
==ignores the order. - 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?
None matches null and undefined, and a Python tuple or float matches the JavaScript array or number that holds the same values, because Python has no undefined and JavaScript has no tuple and one number type. Dictionary key order is never compared. The rest are toggles: tuple against list and int against float when both sides are Python, undefined against null when both are JavaScript, 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.