This morning Daniel Lemire published
Command-line JSON filtering at 20 GB/s.
It introduces sjq, which runs the jq language on top of simdjson.
On his 64-core Xeon, jq needs 11.5 seconds to pull .user.screen_name out of 1 GB of tweets.
sjq does it in 0.31 s on one thread and 0.05 s on 32.
My first thought was: I already have a vectorized, multithreaded JSON engine on my PATH. It’s called DuckDB.
DuckDB 2.0’s CLI reads /dev/stdin and writes to stdout like any other Unix filter.
So I took Lemire’s nine workloads and his data generator, and wrote each jq filter as a SQL query.
Then I timed both tools with input streaming in on stdin.
cat | pipeThe short version: on newline-delimited JSON, DuckDB beats jq by 4.4–7.6× on a single thread, through a plain pipe.
Let it use every core and fed from a file, it reaches 35.8×.
Swap the CLI’s row printer for COPY … TO '/dev/stdout' and the output-heavy jobs reach 56.6×, while it stays a stdin → stdout filter.
The catches turned out to be the most interesting part: a pipe caps it, the CLI’s printer slows it, and the memory use is anything but streaming.
All the scripts and raw hyperfine results are in zorndorff/streaming-duckdb-bench.
The setup
The inputs are the same files Lemire used, built by his make_data.py from simdjson’s twitter.json:
tweets.ndjson: 1 GB, one tweet per line (230,138 lines).timeline.json: one 840 MB pretty-printed{"statuses": [...]}document.
The machine is my desk box, an AMD Ryzen AI MAX+ 395 with 32 threads, running jq 1.8.2, DuckDB v2.0.0 and hyperfine 2.0. It is a different machine from Lemire’s, so don’t compare my numbers with his directly. (jq alone takes 6.9 s here against his 11.5 s.)
The trick on the DuckDB side is not to let it infer a schema.
read_json would build a full typed struct for every tweet, nested entities and all.
read_ndjson_objects hands back each line as a raw JSON value, and the ->> operator pulls out only the path you ask for.
That’s the SQL version of sjq’s “parse only what the filter needs”, and it was about 5× faster than schema inference in my first tests:
# jq -r .user.screen_name
cat tweets.ndjson | duckdb -list -noheader -c \
"SELECT json->>'\$.user.screen_name' FROM read_ndjson_objects('/dev/stdin')"
One DuckDB 2.0 gotcha: the CLI now has an “agent mode”. If an AI-agent environment variable is set and stdout isn’t a terminal, it switches to markdown tables with row caps.
That’s great for Claude and terrible for a Unix filter, so the bench passes -no-agent -no-init explicitly.
Every one of the nine queries is checked against jq with md5 at full size, at 1 and 32 threads, through both a pipe and a redirect. The output is byte-for-byte identical, with a few honest asterisks I cover at the end.
The results
There are two ways to “stream into stdin”, and DuckDB cares a lot about which one you use. I also ran a variant that changes how DuckDB writes, and, for comparison, a setup with no stdin or stdout at all:
- Pipe:
cat file | duckdb …. stdin is a real pipe and can’t be seeked. - Redirect:
duckdb … < file. stdin is still a file descriptor, but the kernel hands DuckDB a regular, seekable file. - Redirect +
COPY: same as redirect, but DuckDB writes withCOPY … TO '/dev/stdout'instead of the CLI’s row printer (explained below). - No stdin: DuckDB reads the path itself (
read_ndjson_objects('tweets.ndjson')) and writes withCOPY … TO 'out.txt'. jq gets the path too and redirects to a file, which is how Lemire ran it.
Use the tabs to switch between them:
jq … file > out), as in Lemire's runs.jq is a flat 0.07–0.16 GB/s whichever way you feed it. DuckDB’s best NDJSON number goes from 1.63 GB/s through a pipe to 5.60 GB/s with a redirect. Reading the path directly is no faster (5.53 GB/s), so a redirected stdin costs nothing. The workloads with big outputs do change, though, and that’s the next surprise.
Full numbers (DuckDB at 32 threads, mean seconds, × vs jq run the same way)
| workload | jq (stdin) | DuckDB, pipe | DuckDB, redirect | DuckDB, redirect + COPY stdout | DuckDB, no stdin |
|---|---|---|---|---|---|
| minify (-c .) | 14.34 s | 1.26 s (11.4×) | 0.96 s (14.8×) | 0.32 s (44.7×) | 0.51 s (29.2×) |
| pretty-print (.) | 15.59 s | 1.30 s (11.9×) | 1.09 s (14.2×) | 0.36 s (42.9×) | 0.64 s (25.5×) |
| extract a field | 6.91 s | 0.66 s (10.5×) | 0.19 s (35.8×) | 0.20 s (35.0×) | 0.19 s (36.1×) |
| select and build objects | 6.95 s | 0.66 s (10.5×) | 0.20 s (35.2×) | 0.20 s (34.3×) | 0.20 s (35.3×) |
| string interpolation | 7.10 s | 0.71 s (10.1×) | 0.26 s (27.4×) | 0.26 s (27.4×) | 0.26 s (27.0×) |
| regular expression | 15.23 s | 0.74 s (20.7×) | 0.66 s (23.1×) | 0.27 s (56.6×) | 0.28 s (55.3×) |
| count elements (doc) | 6.50 s | 2.45 s (2.6×) | 2.17 s (3.0×) | 2.07 s (3.2×) | 2.01 s (3.2×) |
| minify (-c .) (doc) | 11.03 s | 2.83 s (3.9×) | 2.56 s (4.3×) | 2.52 s (4.4×) | 2.62 s (4.3×) |
| group_by (doc) | 6.92 s | 2.55 s (2.7×) | 2.24 s (3.1×) | 2.10 s (3.3×) | 2.06 s (3.3×) |
The pipe is the ceiling, and threads past four make it worse
Is the pipe itself too slow? No: cat tweets.ndjson | cat > /dev/null moves the whole gigabyte in 0.16 s, about 6.7 GB/s.
The limit is how DuckDB consumes a non-seekable stream.
My guess, which I haven’t verified in DuckDB’s source: with a regular file every thread can seek to its own byte range, but with a pipe the stream has to be read in order and handed out, and that serial step becomes the bottleneck.
I swept SET threads=N on the field-extraction query:
.user.screen_name) on the 1 GB NDJSON file, best of 3 runs per point. Solid: stdin redirected from the file. Dashed: stdin is a real pipe.With a redirect, it scales nicely up to about 16 threads.
Through a pipe it tops out around 4 threads at roughly 2.4 GB/s, then gets slower as more threads are added.
If you have to pipe into DuckDB, try SET threads=4. On this workload and machine it beat the default, but that’s one query measured best-of-3, so check it on yours.
The other bottleneck is the CLI’s printer
In the last two tabs, minify and regex jump ahead even though reading the file is no faster.
The only other thing that changed is how rows get out.
By default the CLI renders each result row in -list mode.
COPY … TO hands the rows to DuckDB’s CSV writer instead.
With quoting and escaping switched off, that writer produces the same bytes as jq: QUOTE '', ESCAPE '', and a delimiter that never appears.
And COPY can target /dev/stdout, so you keep a Unix filter:
duckdb -c "COPY (SELECT json(json) FROM read_ndjson_objects('/dev/stdin'))
TO '/dev/stdout' (FORMAT csv, HEADER false, QUOTE '', ESCAPE '', DELIMITER '\x1f')" \
< tweets.ndjson > out.ndjson
I re-ran all nine workloads that way, through both a pipe and a redirect, and every output was md5-identical to jq’s. The gain depends on how much the query writes:
With a redirect, minifying 1 GB drops from 0.96 s to 0.32 s (3.38 GB/s, 44.7× jq).
That’s faster than the no-stdin run (0.51 s), which also has to write a 1 GB file.
Regex goes from 0.66 s to 0.27 s (56.6× jq).
Queries with tiny outputs, like extract a field, don’t move.
Through a real pipe, COPY still helps minify (1.26 s → 0.74 s), but the pipe’s read ceiling caps everything else at about 1.7 GB/s.
The single 840 MB document doesn’t care either way.
So the fastest DuckDB filter I found is duckdb < file plus COPY … TO '/dev/stdout': it’s still stdin → stdout, and it matches or beats letting DuckDB own the files.
“Streaming” is doing a lot of work in that sentence
This is the part that surprised me. jq really is a streaming filter: its peak RSS was 4 MiB on every NDJSON workload. DuckDB is a query engine that happens to read a stream:
| workload | jq | DuckDB 1t, pipe | DuckDB 1t, by path | DuckDB 32t, pipe | DuckDB 32t, by path |
|---|---|---|---|---|---|
| 03 extract a field | 4 MiB | 1,205 MiB | 182 MiB | 4,857 MiB | 3,834 MiB |
| 06 regular expression | 4 MiB | 1,346 MiB | 229 MiB | 6,610 MiB | 4,921 MiB |
| 08 minify (-c .) (doc) | 3,357 MiB | 6,285 MiB | 6,077 MiB | 6,287 MiB | 5,487 MiB |
There are two separate effects here.
Through a pipe on one thread, DuckDB’s peak memory is about the size of the input (~1.2 GiB).
Read the same file by path on one thread and it needs only ~200 MiB, so the pipe case looks like DuckDB buffering the stream.
At 32 threads, memory climbs to 4–5 GiB even when reading by path, so most of that is the parallel scan’s per-thread buffers, not stdin.
… | head -1 doesn’t save you either: it still read the full gigabyte, in the same time and the same ~5 GB of memory.
Writing with COPY … TO '/dev/stdout', or setting preserve_insertion_order=false, didn’t change memory.
If memory matters, SET threads is the knob.
For a 1 GB file on a 128 GB box that’s fine. For an endless tail -f log, reach for jq or sjq.
One big document: modest wins, and one trap
On the single 840 MB timeline.json, DuckDB wins by 3.0–4.3× and gets no benefit from threads.
That makes sense: it’s one JSON value, so it’s one parse.
The trap was group_by. My first version did the natural SQL thing: unnest the statuses array, then GROUP BY.
It produced the correct answer… in 32.1 seconds, which is nearly five times slower than jq, with most of that time spent in the kernel.
Pulling the strings out takes 2.5 s; the unnest cost the other 30.
My best guess, which I haven’t proven, is that each chunk unnest emits drags the 840 MB source value along with it.
The fix is to never unnest at all and aggregate inside the list:
SELECT to_json(list_transform(
list_sort(map_entries(list_histogram(
json_extract_string(json, '$.statuses[*].user.lang')))),
lambda e: {'lang': e.key, 'n': e.value}))
FROM read_json_objects('/dev/stdin', format='unstructured',
maximum_object_size=4000000000);
.statuses | group_by(.user.lang) on the single 840 MB document, piped in. Lower is better. Same output, byte for byte.Fine print
- Pretty-print (02): DuckDB’s
json_prettyindents by 4 spaces. Its output is byte-identical tojq --indent 4 ., but the benchmark times plainjq .. - Minify (01, 08): these use
json(json), which really parses and re-serializes. A bareSELECT jsonwould be a passthrough and would only “match” because the input is already compact. - Regex (06): DuckDB’s optimizer quietly rewrites
regexp_matches(t, 'rt')intocontains(t, 'rt'), so the headline number isn’t a regex at all. Forcing RE2 with the equivalent pattern'(rt)'gives the same output and was actually faster: 1.07 s on 1 thread and 0.45 s on 32, both with a redirect. Also,lower()is Unicode-aware while jq’sascii_downcaseisn’t. They agree on this data, but not on every input. - Equivalence is per dataset. The SQL matches jq on these files, but NULL printing, missing fields and malformed lines would behave differently.
- No sjq row. I didn’t run sjq on this machine, so I can’t put it on these charts. The harness takes
SJQ=/path/to/sjqif you want the real head-to-head. - Method: hyperfine with 3 runs of jq and 10 of DuckDB, one warm-up each, and the files in page cache. The 1-thread runs aren’t CPU-pinned. In the stdin modes, output goes to
/dev/null; in the no-stdin mode both tools write a real file, which lands in page cache. TheCOPY-to-stdout runs reuse the jq timings from the same input mode.
So, should you alias jq to DuckDB?
No, but you should know it’s there.
jq is still the better language for poking at JSON, and sjq (go read Lemire’s post) is in a different league for raw speed.
What surprised me is how far a general-purpose SQL engine gets just by reading stdin.
It’s 10.5× faster than jq through a pipe, 35.8× when it can seek, and up to 56.6× once it skips the CLI’s printer, with byte-identical output.
And the moment the filter turns into a join, a window function or a GROUP BY over a hundred files, you’re already holding the right tool.
Run it yourself:
git clone https://github.com/zorndorff/streaming-duckdb-bench && cd streaming-duckdb-bench
scripts/make_data.sh && scripts/verify.sh && scripts/run_bench.sh
INPUT=redirect scripts/run_bench.sh # duckdb < file
INPUT=redirect OUTPUT=copy scripts/run_bench.sh # ... + COPY TO '/dev/stdout'
INPUT=file scripts/run_bench.sh # no stdin: read path, COPY TO file