language-bench

JSON parsing

Parse a 19 MB JSON document and aggregate its fields.

io-boundmemory-boundserializationstrings

What is measured

Each iteration receives a different ~19 MB ASCII JSON document of 90,000 records (padded with whitespace to a fixed length) as raw bytes. The timed part parses it and walks the document, returning the number of objects, the sum of all integers, the total length of all string values, and the numbers of true and null values.

Pseudocode

bytes = instance                       # untimed
start timer
doc = parse_json(bytes)
walk doc iteratively:
    objects += 1 for every object; int_sum += every integer
    string_length += length of every string value (not keys)
    count true and null
stop timer

Reveals: Parsing and string-processing throughput including allocation of the parsed tree.

Caveat: Parser library, input size, schema and validation all change the result. Go, Node, Python, Ruby, PHP and C# use their standard-library parsers (C# a read-only document, the others a generic tree). Rust, C, C++ and Java have no JSON in their standard library, so they use a small hand-written recursive-descent parser that aggregates while parsing (no tree is built), which is less work than the others. Lua, OCaml, Haskell and Elixir have no JSON parser in their standard library and use a small hand-written parser; Perl uses the core JSON::PP (pure Perl); Nim uses std/json. Scala builds a value tree with its own parser, Fortran tallies while parsing, and R tokenises each line with one regular expression (base R has no parser).

Results (newest release of each language)

Language / buildTime (× vs best)
C 16 · gcc -O3 -march=nativedefault19.6 ms (1.00×)
C 16 · gcc -O319.7 ms (1.00×)
C 16 · gcc -O222.8 ms (1.16×)
Go 1.27 · GOAMD64=v3 go build574 ms (29.3×)
Go 1.27 · go builddefault583 ms (29.8×)
Python 3.15 · python3default714 ms (36.5×)

Click a language to see the source code, build flags, every session and how the number was computed.