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.

5.6 GB/spull one field out of 1 GB of NDJSON (stdin from file)
35.8×faster than jq on that same job
1.63 GB/sthe same query through a real cat | pipe
4.7 GiBpeak memory for that pipe run (jq: 4 MiB)

The 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 with COPY … 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 with COPY … 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 1.8.2DuckDB, 1 threadDuckDB, 32 threads
0 GB/s1 GB/s2 GB/s3 GB/s4 GB/s5 GB/s6 GB/sminify (-c .)jq 1.8.2 · minify (-c .): 0.07 GB/s (14.335 s)0.07DuckDB, 1 thread · minify (-c .): 0.57 GB/s (1.882 s)0.57DuckDB, 32 threads · minify (-c .): 0.86 GB/s (1.255 s)0.86pretty-print (.)jq 1.8.2 · pretty-print (.): 0.07 GB/s (15.587 s)0.07DuckDB, 1 thread · pretty-print (.): 0.53 GB/s (2.043 s)0.53DuckDB, 32 threads · pretty-print (.): 0.82 GB/s (1.304 s)0.82extract a fieldjq 1.8.2 · extract a field: 0.16 GB/s (6.913 s)0.16DuckDB, 1 thread · extract a field: 0.87 GB/s (1.238 s)0.87DuckDB, 32 threads · extract a field: 1.63 GB/s (0.660 s)1.63select and build objectsjq 1.8.2 · select and build objects: 0.15 GB/s (6.948 s)0.15DuckDB, 1 thread · select and build objects: 0.84 GB/s (1.280 s)0.84DuckDB, 32 threads · select and build objects: 1.63 GB/s (0.659 s)1.63string interpolationjq 1.8.2 · string interpolation: 0.15 GB/s (7.105 s)0.15DuckDB, 1 thread · string interpolation: 0.67 GB/s (1.599 s)0.67DuckDB, 32 threads · string interpolation: 1.52 GB/s (0.706 s)1.52regular expressionjq 1.8.2 · regular expression: 0.07 GB/s (15.231 s)0.07DuckDB, 1 thread · regular expression: 0.54 GB/s (1.992 s)0.54DuckDB, 32 threads · regular expression: 1.46 GB/s (0.737 s)1.46count elements (1 doc)jq 1.8.2 · count elements: 0.13 GB/s (6.500 s)0.13DuckDB, 1 thread · count elements: 0.35 GB/s (2.431 s)0.35DuckDB, 32 threads · count elements: 0.34 GB/s (2.453 s)0.34minify (-c .) (1 doc)jq 1.8.2 · minify (-c .): 0.08 GB/s (11.026 s)0.08DuckDB, 1 thread · minify (-c .): 0.30 GB/s (2.833 s)0.30DuckDB, 32 threads · minify (-c .): 0.30 GB/s (2.834 s)0.30group_by (1 doc)jq 1.8.2 · group_by: 0.12 GB/s (6.915 s)0.12DuckDB, 1 thread · group_by: 0.33 GB/s (2.531 s)0.33DuckDB, 32 threads · group_by: 0.33 GB/s (2.546 s)0.33
Throughput = input bytes ÷ mean wall time (hyperfine, 3 runs of jq, 10 of DuckDB). Higher is better. Same scale on every tab. In the last tab jq also reads the path (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)
workloadjq (stdin)DuckDB, pipeDuckDB, redirectDuckDB, redirect + COPY stdoutDuckDB, no stdin
minify (-c .)14.34 s1.26 s (11.4×)0.96 s (14.8×)0.32 s (44.7×)0.51 s (29.2×)
pretty-print (.)15.59 s1.30 s (11.9×)1.09 s (14.2×)0.36 s (42.9×)0.64 s (25.5×)
extract a field6.91 s0.66 s (10.5×)0.19 s (35.8×)0.20 s (35.0×)0.19 s (36.1×)
select and build objects6.95 s0.66 s (10.5×)0.20 s (35.2×)0.20 s (34.3×)0.20 s (35.3×)
string interpolation7.10 s0.71 s (10.1×)0.26 s (27.4×)0.26 s (27.4×)0.26 s (27.0×)
regular expression15.23 s0.74 s (20.7×)0.66 s (23.1×)0.27 s (56.6×)0.28 s (55.3×)
count elements (doc)6.50 s2.45 s (2.6×)2.17 s (3.0×)2.07 s (3.2×)2.01 s (3.2×)
minify (-c .) (doc)11.03 s2.83 s (3.9×)2.56 s (4.3×)2.52 s (4.4×)2.62 s (4.3×)
group_by (doc)6.92 s2.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:

0123456712481632threads (SET threads=N)GB/sredirect, 1 threads: 1.39 GB/s, peak RSS 180 MiBredirect, 2 threads: 2.44 GB/s, peak RSS 324 MiBredirect, 4 threads: 3.70 GB/s, peak RSS 536 MiBredirect, 8 threads: 5.37 GB/s, peak RSS 1026 MiBredirect, 16 threads: 5.97 GB/s, peak RSS 2011 MiBredirect, 32 threads: 5.97 GB/s, peak RSS 3840 MiB< filepipe, 1 threads: 0.89 GB/s, peak RSS 1206 MiBpipe, 2 threads: 1.53 GB/s, peak RSS 1367 MiBpipe, 4 threads: 2.44 GB/s, peak RSS 1547 MiBpipe, 8 threads: 2.39 GB/s, peak RSS 2027 MiBpipe, 16 threads: 2.15 GB/s, peak RSS 2989 MiBpipe, 32 threads: 1.70 GB/s, peak RSS 4863 MiBcat |pipe peaks at 4 threads
Workload 03 (.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:

CLI prints rows (duckdb -list), stdin redirectCOPY … TO '/dev/stdout', stdin redirectread by path, COPY … TO file
0 s0.5 s1 s1.5 s2 s2.5 s3 sminify (-c .)CLI prints rows (duckdb -list), stdin redirect: 0.96 s0.96 sCOPY … TO '/dev/stdout', stdin redirect: 0.32 s0.32 sread by path, COPY … TO file: 0.51 s0.51 spretty-print (.)CLI prints rows (duckdb -list), stdin redirect: 1.09 s1.09 sCOPY … TO '/dev/stdout', stdin redirect: 0.36 s0.36 sread by path, COPY … TO file: 0.64 s0.64 sregular expressionCLI prints rows (duckdb -list), stdin redirect: 0.66 s0.66 sCOPY … TO '/dev/stdout', stdin redirect: 0.27 s0.27 sread by path, COPY … TO file: 0.28 s0.28 sminify (-c .) (1 doc)CLI prints rows (duckdb -list), stdin redirect: 2.56 s2.56 sCOPY … TO '/dev/stdout', stdin redirect: 2.52 s2.52 sread by path, COPY … TO file: 2.62 s2.62 s
DuckDB at 32 threads, mean wall time, lower is better. Output size: minify 1.07 GB, pretty-print 1.48 GB, regex 59 MB, document minify 622 MB. All three paths give byte-identical output.

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:

workloadjqDuckDB 1t, pipeDuckDB 1t, by pathDuckDB 32t, pipeDuckDB 32t, by path
03 extract a field4 MiB1,205 MiB182 MiB4,857 MiB3,834 MiB
06 regular expression4 MiB1,346 MiB229 MiB6,610 MiB4,921 MiB
08 minify (-c .) (doc)3,357 MiB6,285 MiB6,077 MiB6,287 MiB5,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);
0 s5 s10 s15 s20 s25 s30 s35 sjq 1.8.2jq 1.8.2: 6.92 s6.9 sDuckDB: unnest + GROUP BYDuckDB: unnest + GROUP BY: 32.09 s32.1 sDuckDB: list_histogramDuckDB: list_histogram: 2.55 s2.5 s
.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_pretty indents by 4 spaces. Its output is byte-identical to jq --indent 4 ., but the benchmark times plain jq ..
  • Minify (01, 08): these use json(json), which really parses and re-serializes. A bare SELECT json would be a passthrough and would only “match” because the input is already compact.
  • Regex (06): DuckDB’s optimizer quietly rewrites regexp_matches(t, 'rt') into contains(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’s ascii_downcase isn’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/sjq if 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. The COPY-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