Part 14 of 20 in Tools we built and why. Previous: The Mojo libraries: a shelf of small, checked parts · Next: mojo-tls: secure connections with no OpenSSL
What this is about
A byte is the smallest piece of a file that a computer stores, a number from 0 to 255. Two programs that read the same file and write the same bytes have done exactly the same job. That is a strong test, and an easy one to run.
Our Mojo libraries for JSON, CSV and Base64 aim at it. Give them the same input as Python's own libraries, and they must produce the same bytes. If they do not, the library is wrong. Python is the reference, which we call the oracle.
Why it matters
Two programs that disagree by one byte can break a pipeline. A JSON file written with a space after a colon hashes differently from one written without. A CSV field with a quote in it may be read as one field by one library and as two by another. When you replace one library with another, you want nothing to change but the speed.
There is a second reason. When the bar is "same as Python", the check is easy to run and easy to understand. You do not have to trust us. You can run Python on the same file and compare.
How it works
The idea is the same for every library: run the input through both, and compare what comes out.
mojo-json
The parser follows RFC 8259 strictly. We test it with JSONTestSuite, a public set of test files. Files that must be accepted are named with y_. Files that must be rejected are named with n_. Our parser accepted 95 of 95 of the y_ files with values identical to Python's, and rejected 188 of 188 of the n_ files.
The writer is checked against Python's json.dumps on generated files in four layouts. It matches byte for byte, including how floats are printed. One small detail shows the care: integers larger than a 64-bit number become floats, rounded the way Python's float(int) rounds.
mojo-csv
The reader and writer follow the quoting rules of CPython 3.14. Unquoted fields are views into the input, so no copy is made. On 50 MB of mixed records, the reader streamed at 227.50 MB/s against CPython at 85.79 MB/s. The write_all call ran at 468.2 MB/s against 97.1 MB/s. The receipts for these runs are in the repository's benchmark folder.
mojo-base64
The oracle tests compare the bytes and the full error messages with CPython 3.14.8 and 3.14.9. The library handles Base16, Base32, Base32hex and Base64. On one pinned core, Base64 encoding ran at 1.23 GB/s against CPython at 0.77 at 64 MiB. Decoding ran at 1.28 against 0.64.
An honest note: peak memory of the Base64 benchmark was 7.7% above CPython's, 270 MB against 251 MB, and the README says the cause is allocator retention. Base32 is not tuned for speed.
The order matters
Notice the order in each section. First, the output must match. Only then do we report speed. A fast library that returns different bytes is not a faster copy. It is a different program. Our rule is that a speed number is not reported from a build that has not passed the match.
How to do it, step by step
Here is how you could check one yourself.
- Install the library, for example
pixi add mojo-jsonafter adding the amarbaro channel as shown on the libraries page. - Take a JSON file of your own.
- Parse it with Python and print it with
json.dumps. Save the result. - Parse it with
mojo-jsonand print it withdumps(False), which uses Python's default layout. Save the result. - Compare the two files with
cmporsha256sum. They should be identical. - Do the same with a file you wrote badly on purpose, with a trailing comma. Both should refuse it.
The quick example in each README does the first half of this for you.
What this does not prove
- Matching on test files is not matching on all inputs. The limits are written in each README. For example, mojo-base64 does not cover encode altchars, string inputs, Base85 or wrapped-line APIs.
- The version of Python matters. We name the versions we compared with.
- Speed numbers come from one machine. Treat them as examples.
- Same bytes does not mean same behavior everywhere. Error type names differ, because Mojo errors are not Python exceptions.
Words you may not know
- Byte: one number from 0 to 255 stored in a file.
- Oracle: the trusted reference we compare with. Here it is Python.
- Parser: a program that reads text and understands its structure.
- RFC 8259: the public specification of JSON.
- Base64: a way to write binary data using only letters and digits.
- Parity: matching the reference in every tested case.
- Receipt: a saved record of a measurement that you can open.
Try it
- Open mojo-json, mojo-csv and mojo-base64 and find the line in each README that names the oracle.
- See all the libraries on the Mojo libraries page.
- Run the comparison from the steps above on a file of your own.
- Read how we test a tool before we share it.
Read next
- The Mojo libraries: a shelf of small, checked parts: the whole shelf.
- mojo-tls: secure connections with no OpenSSL: the next post in this series.

Comments
No comments yet.
Log in to comment.