Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Performance

jlens is a tree-walking interpreter, like jsonata-js. This chapter says how fast it is against the other JSONata engines on each platform, how that is measured, and how to use the API to get the most from it.

Summary

Measured for 0.37.0 on an Apple M1 (below), on seventeen expressions over a 24 KB order and an array of 10,000 items:

PlatformAgainst the referenceAgainst the fastest other engine
Rustcustomer.name in 0.10 µs, a 30-line rule in 42 µs against a Document–
Node.js (WebAssembly)faster than jsonata-js on 15 of 17 cases with evaluate, all 17 against a Document, by up to 520×jsonata2js, which compiles to JavaScript, is faster on 14 of 17
Pythonfastest of the Python packages on 14 of 17 cases with Python values, 15 of 17 against a Document; 18 to 226× jsonata-pythonjsonata2py leads on names over items and the index into 10,000 items, jsonatapy on that index once converted
Javafaster than jsonata-java on all 17 cases against a Document, the most evaluations per second from JSON text on any number of threadsjsonata-jvm-compiler, which compiles to bytecode, is faster on most cases over converted input
  • jlens is fastest where the expression reads a little of a large input (an index, a path) and where an input converted once into a Document serves many evaluations.
  • The engines that compile an expression to the host language’s code (jsonata2js, jsonata2py, jsonata-jvm-compiler) are faster on most cases that do much work per item. jlens is an interpreter, and the same on every platform.
  • On deep, cyclic or endless input, jlens fails with a JSONata error code and never ends the process (Python, Java).

The numbers are meant for comparing alternatives on one machine. They are not guarantees, and your hardware will give different absolute values. How each change was measured (methods, traps, intermediate numbers) is kept outside the book, in docs/notes/: perf-0.37.md, perf-0.36.md, perf-0.35.md, perf-0.34.md, perf-0.33.md, perf-0.32.md, perf-0.31.md, perf-0.30.md, perf-0.29.md, perf-0.28.md, perf-0.27.md, perf-0.26.md and perf-earlier.md.

Benchmarks

The workload is in benches/ at the repository root. It is shared by the native benchmarks and by the comparisons with jsonata-js, with the other Python packages and with the JVM implementations:

  • benches/cases.json lists seventeen expressions: path navigation, a name mapped over the items, filters with aggregation, grouping, sorting, string and regex functions, number formatting, an object constructor, a transform, a recursive function, the shipping rule from the guides, two indexes into a long array (one of them held in a variable), a regex over a 20 KB text, $distinct over the items, and a path with tuples.
  • benches/orders-200.json is a 24 KB store order with 200 items, the input of most cases.
  • benches/items-10000.json is an array of 10,000 small items, the input of the two index cases, items[0].code and ($i := 0; items[$i].code): they cost little only when an index does not touch the other items, which 200 items would hide.
  • benches/growth.json lists the cases of cargo xtask growth, which times each built-in function and operator at 1,000 to 64,000 items and prints the exponent of its growth, 1 for linear and 2 for quadratic (where the time goes).
  • crates/jlens-core/benches/host_state.rs builds a document from Rust values again and again, as an application that keeps its state in Rust types does (below). It is native only.
cargo bench -p jlens-core                     # criterion, native
cargo xtask bench-table                       # the native table below: the median of three runs
cargo xtask ab main .                         # two revisions against each other, native
cargo xtask wasm && cd benches/compare && npm install && node compare.mjs   # against jsonata-js
python benches/compare/python.py speed jlens=<python> ...   # against the Python packages
python benches/compare/python.py ab A=<python> B=<python>   # two builds of the Python package
cargo xtask java-compare                      # against the JVM implementations (benches/compare/README.md)
cargo xtask java-compare ab <A.jar> <B.jar>   # two builds of the Java package
cargo bench -p jlens-regress --bench engine   # the regular expression engine alone (below)
cargo xtask growth                            # how each built-in's time grows with its input

The numbers below were measured for jlens 0.37.0 on a MacBook Air M1 (4+4 cores, 16 GB, no fan) under macOS 27, with Rust 1.100, Node.js 24, Python 3.14.10 and OpenJDK 25.0.5.1; the system’s thermal pressure stayed nominal through the runs. The columns of other engines whose version has not changed keep the numbers measured there for 0.31.0, the first release measured on it, or for the release that measured their version first.

A table that names an earlier release was measured for that release on an AMD Ryzen 7 3700X under Windows 11 (Linux: Ubuntu 26.04 under WSL 2), where every table up to 0.30.0 came from, and is not measured again: its absolute times are about twice the M1’s. x86_64 has no tables of its own. A release whose plan changed the evaluation is compared with the release before it on that machine, natively and in Python under Linux, and what that finds is in the release’s notes.

Comparing two revisions

To measure a change, compare the revision before it with the revision that makes it. Criterion alone can mislead here: on some machines its results are bimodal, so two runs of one build can differ by more than the change. cargo xtask ab is built for this:

cargo xtask ab main .                         # main against the working tree
cargo xtask ab v0.9.0 HEAD --case regex --case strings --rounds 20
cargo xtask ab main . --mode document         # evaluate_document instead of evaluate
cargo xtask ab main . --mode source           # evaluate_source instead of evaluate
  • A revision is anything git names, or . for the working tree. Each is built once with the release profile, in a temporary worktree, and kept in target/ab/bin.
  • --mode picks how the cases read their input: a serde_json::Value (evaluate, the default), a Document converted beforehand (document), or a Source written in the program, one level at a time, as the Python and Java packages read their values (source).
  • It runs the two programs in alternation, ten rounds by default (--rounds), A then B, then B then A, so that a drift of the machine reaches both, each on a new copy of its program. It prints the median of the rounds for each case and how much slower B is. Judge by the median.
  • Run one comparison at a time: two at once disturb each other, by 8% on the Ryzen.

A time also follows the memory a program’s code was given, which stays with a file while it is cached: six copies of one program differed by up to 5.6% on Windows, and by 10% on one case under Linux. The new copy for each round turns that into spread, and a revision compared with itself shows the floor of a comparison: most cases within 1.7%, the transform within 2.9% and the object constructor within 4.1% on Windows; within 2.6% under Linux; within 0.8% on the M1.

Code placement moves cases too: a change can make a case that runs none of its code 5 to 8% slower or faster, as the release profile (LTO, one codegen unit) lays the whole program out again. To set that aside, build both programs with functions and blocks aligned, as the engine’s benchmark does, through RUSTFLAGS; ab names its programs by commit, not flags, so delete the commit’s program before an aligned run and after it.

The Python and Java packages are compared the same way with python.py ab and cargo xtask java-compare ab, five rounds by default. * marks a difference on which no run of one build reaches the other’s, above 3.5% in Python and 5% in Java (benches/compare/README.md). The tables below come from one build each, so a change of a few percent from an earlier table can be that build’s; the release compares builds as above (0.37 against 0.36.0).

Native (criterion)

The table is measured with cargo xtask bench-table, in about ten minutes. It builds the program of cargo bench -p jlens-core --bench evaluate once, with the release profile and no alignment flags, as an application builds it, runs a new copy of it three times, and prints each case’s time in each run and their median. A case where most runs are more than 2% from the median, or whose runs fall in two groups more than 5% apart, is marked and run again with --runs 5. A case more than 2% away from the previous table is compared with cargo xtask ab <previous tag> ., built aligned, before the difference is put down to a change.

Each evaluate call reads its serde_json::Value input as the expression reaches it, one object or array at a time (ADR 0010); evaluate_document evaluates against a Document converted beforehand. With macOS’s system allocator:

Caseevaluateevaluate_document
path (customer.name)0.24 µs0.10 µs
names over items ($sum(items.quantity))7.0 µs1.71 µs
filter + aggregate26 µs17.2 µs
group-by48 µs39 µs
sort + map165 µs148 µs
strings42 µs32 µs
regex59 µs50 µs
number formatting12.3 µs11.0 µs
object constructor85 µs75 µs
transform189 µs169 µs
recursion ($f(15), Fibonacci)232 µs233 µs
shipping rule60 µs42 µs
index into 10,000 items (items[0].code)0.52 µs0.29 µs
index in a variable (($i := 0; items[$i].code))0.66 µs0.41 µs
regex over a long text (20 KB)88 µs87 µs
distinct items50 µs10.7 µs
tuples (items#$n@$i[...])76 µs68 µs

Three copies, which agreed within 1.9% (recursion with evaluate, group-by against a Document). On Windows, the system allocator makes some cases draw one of two times for each process (recursion against a Document 776 or 890 µs on the Ryzen); mimalloc, which the command and the packages use, does not draw.

Other measurementsTime
Compile customer.name0.64 µs
Compile the shipping rule (30 lines)21 µs
Document::parse of the 24 KB document66 µs
Document::new from a serde_json::Value38 µs
serde_json::from_str of the same text, for reference114 µs
Shipping rule, audit level decisions770 µs (13× without a tracer)
Shipping rule, audit level full3.73 ms (62× without a tracer)

Without a tracer, evaluation takes the shortcuts described under Where the time goes; with one it takes the general path, so that the audit reports every node, hence the large ratios.

The path case reads one field of the input, customer, and one of that object, name: evaluate takes 0.24 µs, where converting the whole input first took 38 µs. A path step reads only the field it looks up (ADR 0010, amended), so names over items reads one field of each item and takes 7.0 µs. An expression that reads whole items pays for converting them, and the allocator sets much of that cost; an application that calls evaluate on whole documents can set a faster global allocator, as the jlens command does.

The allocator of the command

Evaluation allocates for most of its work, so the allocator sets much of its speed. The jlens command uses mimalloc as its global allocator on every release target, and so do the Python and Java packages. cargo xtask ab . . --b-allocator mimalloc measures the allocator alone (B is the same revision with mimalloc), and --target x86_64-unknown-linux-musl builds both programs for musl, as the Linux release binaries are. Measured for 0.17 on the Ryzen with evaluate: musl’s own allocator is the slowest of the three, up to 5.6 times a glibc build, and mimalloc makes it 28 to 67% faster; on Windows, mimalloc saves 17 to 52% (the transform 1.18 ms against 604 µs); glibc’s allocator is the fastest, and mimalloc still saves 3 to 20% on ten cases of twelve. On macOS, the system allocator was not compared with it. Until 0.26, every evaluation on musl’s main thread ran on a new stack segment (stacker#155); jlens works around it, and path takes 339 ns there with mimalloc.

WebAssembly against the other engines (Node.js)

All run in the same Node.js process, on the same input. The WebAssembly module is the release build optimized by wasm-opt -Os, with link-time optimization since 0.27. The comparison script first checks that each engine gives jsonata-js’s result for each case, and all do. The jsonata-js times include the await that its asynchronous evaluate requires. jsonata2js translates an expression to JavaScript source and lets the JavaScript engine compile it. benches/compare/README.md says how each is called.

Measured for 0.37.0, the median of four runs, each on a new copy of the module, which agreed within 3%, but jsonata-js, up to 7% apart on recursion, and the times under 4 µs, printed to a tenth; jsonata2js for 0.31.0, as its version has not changed since. jsonata-js 2.3.0 is within 2.3% of its numbers for 0.36.0:

Casejsonata-js 2.3.0jsonata2js 0.1.2jlens-wasm evaluatejlens-wasm evaluateJsonjlens-wasm evaluateDocument
path1.3 µs0.2 µs2.4 µs72 µs0.7 µs
names over items42 µs3.2 µs29 µs101 µs3.7 µs
filter + aggregate417 µs8.8 µs87 µs146 µs27 µs
group-by649 µs13.8 µs125 µs175 µs55 µs
sort + map3.00 ms61 µs292 µs353 µs236 µs
strings372 µs11.1 µs127 µs190 µs53 µs
regex381 µs10.1 µs118 µs182 µs77 µs
number formatting336 µs75 µs42 µs124 µs16.9 µs
object constructor753 µs52 µs151 µs282 µs87 µs
transform819 µs204 µs354 µs496 µs191 µs
recursion5.59 ms42 µs365 µs430 µs358 µs
shipping rule1.12 ms27 µs194 µs205 µs65 µs
index into 10,000 items1.3 µs145 µs3.4 µs2.31 ms0.9 µs
index in a variable1.97 ms197 µs3.8 µs2.31 ms1.1 µs
regex over a long text346 µs135 µs184 µs184 µs158 µs
distinct items6.47 ms56 µs178 µs167 µs16.7 µs
tuples652 µs28 µs155 µs215 µs95 µs
  • evaluate is faster than jsonata-js on fifteen of the seventeen cases, by up to 15× on recursion, 36× on distinct items, whose $distinct is quadratic in jsonata-js, and 520× on the index held in a variable, which jsonata-js tests against every item. It is slower on path and the index into 10,000 items, which do almost no work, so the overhead of calling into WebAssembly dominates.
  • Against a Document converted once, jlens-wasm is faster than jsonata-js on all seventeen cases: by 12 to 20× on recursion, group-by, sort + map, filter + aggregate, number formatting and the shipping rule, 390× on distinct items, 4.3× on the transform and 1.4× on the index into 10,000 items.
  • jsonata2js is faster than evaluate on fourteen of the seventeen cases, and than evaluateDocument on twelve: by 1.2 times on names over items and regex over a long text to 8.5 times on recursion. As compiled JavaScript, it reads the JavaScript objects it is given, with nothing to convert. jlens-wasm is faster on number formatting, the two indexes into 10,000 items, and, against a Document, distinct items and the transform. Called again on the same text, its compile takes 2 to 4 times what jlens’s new Expression takes, which it makes up within a few evaluations.
  • jsonata-js works on the JavaScript objects it is given, with nothing to convert. jlens-wasm evaluate reads the input object as the expression reaches it: up to 16 values in one call to a JavaScript helper, a long array of objects in one call with the fields the expression reads, and an index the items it picks alone. An expression that reads every item whole pays what converting the whole input does: JSON.stringify takes 27 µs, parsing it into a Document 136 µs.
  • evaluateJson takes JSON text and returns JSON text, and parses the input without the fields the expression does not read (ADR 0016). Parsing still costs far more than a small evaluation: 72 µs on path, and 2.3 ms for the text of 10,000 items. An input evaluated more than once is best converted once into a Document.

gnata-js 0.6.2, the Go engine gnata built for WebAssembly, left the table after 0.27.0: it was slower than jlens-wasm on every case.

Python against the other implementations

benches/compare/python.py compares the Python package with three other JSONata packages on PyPI: jsonatapy, the Python package of jsonata-core, a JSONata engine in Rust; jsonata2py, which translates an expression to Python source and compiles it; and jsonata-python, a port of jsonata-java in pure Python, by far the most downloaded. Each package needs a virtual environment of its own, as several install a module named jsonata; the script’s docstring says how to run it. Each package gives jlens’s result on each case but regex over a long text, where jsonata2py and jsonata-python give 4,150, not the 9,858 of jsonata-js and jlens: Python’s \w matches the accented, Greek and Japanese letters that JavaScript’s does not.

Measured for 0.37.0, on Python 3.14.10; jsonata2py for 0.31.0, as its version has not changed since. jsonatapy 2.2.17 was built from its source distribution when installed. The input is the document as Python values, and for jlens and jsonatapy also an input converted once (Document, JsonataData). jlens and jsonatapy are the median of three runs, which agreed within 2.5%, but jsonatapy 5.4% apart on the transform converted once; jsonata-python, one run, within 1.4% of its numbers for 0.36.0. jsonatapy 2.2.17 is within 3.5% of 2.2.15 but on the transform, 29% faster (724 → 511 µs):

Casejsonata-python 0.7.2jsonatapy 2.2.17jsonatapy, JsonataDatajsonata2py 0.1.3jlens evaluatejlens evaluate_document
path1.06 ms0.5 µs0.2 µs0.5 µs0.5 µs0.4 µs
names over items1.40 ms15.0 µs3.6 µs13.2 µs13.7 µs2.0 µs
filter + aggregate3.28 ms58 µs35 µs62 µs38 µs17.3 µs
group-by4.68 ms88 µs60 µs151 µs68 µs38 µs
sort + map17.4 ms302 µs226 µs266 µs170 µs128 µs
strings3.10 ms81 µs54 µs64 µs49 µs28 µs
regex3.24 ms91 µs73 µs158 µs59 µs43 µs
number formatting2.85 ms70 µs67 µs90 µs12.6 µs9.6 µs
object constructor4.11 ms155 µs124 µs182 µs87 µs60 µs
transform3.92 ms511 µs429 µs347 µs204 µs132 µs
recursion28.8 ms593 µs593 µs1.39 ms233 µs233 µs
shipping rule7.03 ms166 µs100 µs221 µs100 µs42 µs
index into 10,000 items20.3 ms183 µs0.4 µs0.7 µs0.8 µs0.5 µs
index in a variable36.2 ms625 µs440 µs1.29 ms0.9 µs0.6 µs
regex over a long textdiffers93 µs94 µsdiffers88 µs88 µs
distinct items1.46 ms511 µs410 µs307 µs80 µs10.7 µs
tuples4.35 ms264 µs237 µs277 µs83 µs64 µs
  • With Python values in, jlens is the fastest on fourteen of the seventeen cases: by 5.6 times on number formatting, 3.8 times on distinct items, 3.2 times on tuples, 2.5 times on recursion, 690 times on the index held in a variable, which jsonatapy and jsonata2py test against every item, and by 1.1 to 1.8 times on the rest. It is level with both on path (0.5 µs), and jsonata2py is ahead on names over items (13.2 against 13.7 µs) and the index into 10,000 items (0.7 against 0.8 µs).
  • With the input converted once, jlens is the fastest on fifteen of the seventeen cases, by 7.0 times on number formatting and 38 times on distinct items; jsonatapy is faster on path (0.2 against 0.4 µs, printed to a tenth) and on the index into 10,000 items (0.4 against 0.5 µs).
  • jsonata2py reads the Python values it is given, with nothing to convert, as compiled Python. Besides path and the two cases where it leads, it takes 1.3 to 7.1 times what jlens evaluate takes, and 1,400 times on the index in a variable.
  • jsonata-python takes 18 to 226 times what jlens evaluate takes on every case but path and the two indexes, and reads the whole input before evaluating: 1.1 ms on path and 20 ms on the index into 10,000 items.

jsonata-rs 0.1.4, a port of jsonata-java in Rust, left the tables after 0.27.0: it was slower than jlens on every case, and crashed the interpreter on deep and cyclic input. jsonata, which runs jsonata-js in the Duktape interpreter, is left out: no release since 2023, and its module cannot load jsonata-js on Windows.

Limits in Python

How each behaves at the limits, measured by python.py limits for 0.27.0 under Linux, with the packages of the table above; jsonatapy 2.2.17 gave the same answers for 0.37.0. Each case runs in a process of its own, killed after 30 seconds. $string($) reads the whole input, and the cyclic input is {"n": 1, "child": {"parent": <the input>}}:

Casejsonata-pythonjsonatapyjsonata2pyjlens
Input nested 900 levels, $string($)resultresultU1001result
Input nested 2,000 levelsresultresultU1001U1001
Input nested 100,000 levelsRecursionErrorresultU1001U1001
Cyclic input, nRecursionError111
Cyclic input, $string($)RecursionErrorruns onU1001U1004
Unbounded recursion, 1 + $f($n + 1)RecursionErrorU1001U1001D1011
The same, with a depth limit of 500D1011D1011no such optionD1011
Endless tail call, $f($n + 1)runs onruns onU1001 after 0.09 sruns on
The same, with a timeout of 1 sD1012 after 1.0 sD1012 after 1.0 sU1001 after 0.09 sD1012 after 1.0 s
  • jlens fails with a JSONata error in each case, and never ends the process. It refuses input nested more than 1,000 levels, which the others but jsonata2py accept as long as their stack or Python’s recursion limit lasts: a fixed limit that fails the same way on every platform, at the cost of inputs between 1,000 levels and that point.
  • jlens, jsonatapy and jsonata2py meet a cycle only when the expression reaches it; jsonata-python reads the whole input first, so n fails too.
  • jsonata2py stops each case with U1001, “stack overflow”, within 0.1 s. Its limits are low: probed on Windows, it accepts input nested up to 495 levels, a recursion such as 1 + $f($n + 1) up to 99 calls deep, and loops of 100,000 tail calls but not of 1,000,000.
  • No package stops an endless evaluation by default: set a timeout.

Java against the other implementations

cargo xtask java-compare compares the Java package with two other JSONata implementations for the JVM on Maven Central: jsonata-java, a port of jsonata-js to Java objects, and jsonata-jvm-compiler, which translates an expression to Java and compiles it with javac at run time, over Jackson trees. benches/compare/README.md says how to run it, and which packages it leaves out and why. jsonata-java gives jlens’s result on every case, jsonata-jvm-compiler on all but two: on tuples it gives arrays of the products where jsonata-js gives their sums, and on regex over a long text 4,150, as its \w is Python’s.

Measured for 0.37.0, on OpenJDK 25.0.5.1 (8 hardware threads), every engine in one JVM with -Xmx4g; jsonata-jvm-compiler for 0.34.0, as its version has not changed since. Each time is the best of five runs of 0.3 s after a second of warm-up, in the faster of two JVMs, as jsonata-java ran filter + aggregate 30% slower in one of them. Each engine returns its own objects, not serialized: jlens maps, lists and boxed values. With the input converted once: Java maps for jsonata-java and evaluate(Object), a Jackson tree for jsonata-jvm-compiler, and a Document for evaluate(Document):

Casejsonata-java 0.9.11jsonata-jvm-compiler 1.0.7jlens evaluate(Object)jlens evaluate(Document)
path8.3 µs0.03 µs0.89 µs0.21 µs
names over items16.0 µs1.3 µs16.3 µs1.8 µs
filter + aggregate59 µs4.1 µs51 µs16.8 µs
group-by121 µs13.1 µs77 µs38 µs
sort + map443 µs37 µs169 µs130 µs
strings69 µs7.4 µs70 µs27 µs
regex95 µs11.9 µs64 µs43 µs
number formatting92 µs8.5 µs15.5 µs9.7 µs
object constructor104 µs6.9 µs95 µs58 µs
transform236 µs27 µs211 µs126 µs
recursion775 µs44 µs245 µs231 µs
shipping rule161 µs15.2 µs127 µs41 µs
index into 10,000 items151 µs0.04 µs1.2 µs0.36 µs
index in a variable660 µs6.8 µs1.3 µs0.46 µs
regex over a long text99 µsdiffers116 µs88 µs
distinct items22 µs18.2 µs94 µs10.7 µs
tuples151 µsdiffers95 µs64 µs
  • jsonata-jvm-compiler is the fastest on every case it agrees on but the index in a variable and distinct items, by 1.1 (number formatting) to 8.4 times (object constructor) against a jlens Document on the cases that take a microsecond or more, 5.2 times on recursion: its expression is a Java class, which the JIT compiles with the application.
  • Against jsonata-java, the other interpreter, jlens over a Document is faster on every case. It takes less than a third of the time on recursion, sort + map and the shipping rule, and less than a ninth on number formatting.
  • Over Java maps, jlens takes 1.3 to 3.1 times as long as over a Document on the cases that read the order’s items, and 9 times on names over items and distinct items: each value read is a call back into Java, though a map is written with only the fields the expression reads (ADR 0021, amended). jsonata-java is then faster on distinct items, which reads whole items, and level on names over items and strings.

From JSON text, parsed at each evaluation, by its own parser for jsonata-java, by Jackson for jsonata-jvm-compiler, and by evaluateFromJson for jlens, which skips the fields the expression does not read:

Casejsonata-java 0.9.11jsonata-jvm-compiler 1.0.7jlens evaluateFromJson
path93 µs70 µs31 µs
names over items101 µs72 µs41 µs
filter + aggregate144 µs75 µs64 µs
group-by212 µs86 µs85 µs
sort + map526 µs109 µs175 µs
strings154 µs80 µs80 µs
regex182 µs84 µs85 µs
number formatting168 µs82 µs53 µs
object constructor189 µs80 µs107 µs
transform318 µs98 µs183 µs
recursion1.10 ms116 µs270 µs
shipping rule251 µs88 µs100 µs
index into 10,000 items1.45 ms1.38 ms882 µs
index in a variable1.98 ms1.33 ms882 µs
regex over a long text118 µsdiffers117 µs
distinct items108 µs93 µs69 µs
tuples240 µsdiffers110 µs

jlens is the fastest where the parsing is the cost (path, names over items, filter + aggregate, number formatting, distinct items and the two index cases), and level with jsonata-jvm-compiler on group-by, strings and regex; jsonata-jvm-compiler where the evaluation is, 116 against 270 µs on recursion. jlens is faster than jsonata-java on every case but regex over a long text, where they are level.

On the documents of ADR 0021’s spike, 1,000 and 100,000 items of five fields each, an index or $count takes the same time at both sizes in jlens, 0.3 to 0.5 µs against a Document, where jsonata-java takes 57 µs and 7.1 ms for items[0].name. From JSON text, jlens takes about half the time of the others on 100,000 items, but on $, for which it builds the whole document as Java objects.

Evaluations per second of $sum(items.price) on the JSON text of 100 items (9.5 KB), on 1 to 8 platform threads (four of the M1’s eight cores are efficiency cores) and on 1,000 virtual threads, each engine parsing the text at each evaluation; jsonata-java, whose compiled expressions are not thread-safe, compiles one per thread:

Engine1 thread2 threads4 threads8 threads1,000 virtual threads
jlens, JSON text43,424 (1.0×)84,689 (2.0×)162,618 (3.7×)212,267 (4.9×)212,733 (4.9×)
jlens, Java objects parsed by Jackson17,062 (1.0×)33,506 (2.0×)63,887 (3.7×)84,426 (4.9×)85,024 (5.0×)
jsonata-java 0.9.1118,694 (1.0×)36,454 (2.0×)69,624 (3.7×)92,833 (5.0×)93,285 (5.0×)
jsonata-jvm-compiler 1.0.719,501 (1.0×)38,177 (2.0×)73,603 (3.8×)99,066 (5.1×)99,066 (5.1×)

jlens leads at every count of threads, by 2.1 times or more. Parsed by Jackson into Java maps first, the parse, not the evaluation, is the cost.

Limits on the JVM

Measured by cargo xtask java-compare limits under Linux on the Ryzen, with the cases of python.py limits; jsonata-java 0.9.11 gave the same answers as 0.9.10 for 0.36.0. Each case runs in a JVM of its own, on its main thread with the default stack, killed after 30 seconds. jlens reads Java objects (evaluate(Object)), jsonata-java maps, and jsonata-jvm-compiler a Jackson tree, built the same way, since text cannot hold a cycle:

Casejsonata-java 0.9.11jsonata-jvm-compiler 1.0.7jlens
Input nested 900 levels, $string($)resultresultresult
Input nested 2,000 levelsresultresultU1001
Input nested 100,000 levelsStackOverflowErrorStackOverflowErrorU1001
Cyclic input, nStackOverflowError11
Cyclic input, $string($)StackOverflowErrorStackOverflowErrorU1004
Unbounded recursion, 1 + $f($n + 1)StackOverflowErrorU1001D1011
The same, with a depth limit of 500JException (stack overflow, depth 501)no such optionD1011
Endless tail call, $f($n + 1)runs onU1001 after 0.6 sruns on
The same, with a timeout of 1 sJException (timeout) after 1.0 sU1001 after 0.6 sD1012 after 1.2 s
  • jlens fails with a JSONata error code in each case but the endless loop with no timeout. No JVM crashed: a StackOverflowError is thrown to the caller, who must catch an Error rather than an exception to survive it, and it can leave an application’s own state half updated.
  • jsonata-jvm-compiler turns a stack overflow into U1001. It has no tail calls, so the endless tail call stops there too, and so would a loop of 100,000 tail calls. It has no depth limit.
  • jsonata-java takes a depth limit and a timeout (Frame.setRuntimeBounds), and reports them with messages, not codes.

The regular expression engine

jlens-regress, jlens’s fork of regress (ADR 0020), has a benchmark of its own, crates/jlens-regress/benches/engine.rs, whose cases are in engine.json beside it. Each case finds every match of a pattern in a text, as jlens does: from the end of the previous match, without an interrupt, or with one (when the evaluation has a timeout or a cancellation token). The texts are 200 lines of a log (15 KB), a sentence of English repeated to 12 KB, and a sentence of Portuguese, Greek and Japanese repeated to 20 KB of UTF-8. engine.mjs runs the same cases in Node.js, with exec and the flag g, as jsonata-js does.

Each variant is a binary of its own, built with functions and blocks aligned, in a target directory of its own, since two copies of the backtracker in one binary are inlined differently:

export RUSTFLAGS="-C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=5"
export CARGO_TARGET_DIR=target/aligned
cargo bench -p jlens-regress --bench engine            # without an interrupt
JLENS_REGRESS_INTERRUPT=1 cargo bench -p jlens-regress --bench engine
node crates/jlens-regress/benches/engine.mjs           # V8

Each run saves its times and prints what the check costs against the last run of the other variant. A change to the engine is measured the same way, aligned, against the revision before. Measured for 0.37.0 (the median of 31 samples of at least 5 ms, and of three runs without an interrupt and two with one; V8 13.6, Node.js 24.22):

CasePatternMatchesjlens-regressWith an interruptV8
literalstatus=2002005.3 µs5.2 µs (−1.9%)11.2 µs
literal, no matchstatus=5000505 ns508 ns (+0.6%)7.2 µs
case-insensitive literallazy dog, i2006.1 µs6.5 µs (+6.6%)10.0 µs
classes\d{4}-\d{2}-\d{2}T[\d:]+Z20028.2 µs28.5 µs (+1.1%)15.8 µs
classes, no match\d{3}-\d{4}03.5 µs3.5 µs (+0.0%)1.6 µs
alternation of words\b(?:fox|dog|cat)s?\b400136 µs139 µs (+2.5%)15.3 µs
words\b\w+\b2,400175 µs180 µs (+2.9%)77.0 µs
capturesuser=(\w+) id=(\d+)20027.4 µs27.9 µs (+1.8%)11.5 µs
lazypath=(.*?) 20036.2 µs36.4 µs (+0.6%)9.2 µs
lines^.*status=200$, m20028.9 µs29.4 µs (+1.7%)15.1 µs
backreference, no match(\w+)\s+\10485 µs500 µs (+3.1%)150 µs
lookahead\w+(?=,)200410 µs425 µs (+3.7%)139 µs
lookbehind(?<=the )\w+200150 µs165 µs (+9.8%)51.9 µs
Unicode text\S+2,000151 µs156 µs (+3.2%)68.8 µs
Unicode text, case-insensitiveγράμματα, i20022.3 µs22.4 µs (+0.4%)7.4 µs
Unicode property\p{L}+, u2,000210 µs215 µs (+2.5%)331 µs
anchored, short text^1[0-9]$ on 17141 ns45 ns (+9.8%)37 ns
CompilationPatternjlens-regressV8 new RegExpV8, and a first exec
date-time^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})$6.5 µs1.6 µs14.9 µs
e-mail, case-insensitive^[a-z0-9._%+-]+@(?:[a-z0-9-]+\.)+[a-z]{2,}$, i7.4 µs1.1 µs24.9 µs
Unicode property\p{L}+, u3.2 µs24.5 µs55.0 µs
  • V8 compiles a pattern to machine code, and is 1.8 to 9 times faster wherever the engine runs its instructions at many positions of the text: alternations, classes, lookarounds, backreferences. jlens-regress is a backtracker that interprets its instructions. It is faster where it can search for a literal first (memchr): 2.1 times on literal, 14 times on a literal that does not occur; and on \p{L}+.
  • jlens compiles a regex literal once per compiled expression, the first time an evaluation reaches it, and every later evaluation, on any thread, uses that pattern (since 0.37; before, each evaluation compiled it again). V8’s new RegExp only parses the pattern; the first exec compiles it.
  • The interrupt costs about 2.8% over these 17 cases (the geometric mean), from 1.9% faster to 9.8% slower per case (lookbehind and anchored, short text, 10% in each run), most of that spread from how the compiler allocates the backtracker’s registers, not from the check itself.

Through jlens, $match, $replace, $split and $contains are linear in the text since 0.27, and with a regex literal they build only what their result holds: on 20 KB of text with 1,558 matches of /\w+/, $replace takes 0.23 ms against 0.48 ms in jsonata-js, and $match, which returns an object for each match, 0.81 ms against 0.36 ms (measured for 0.28 against a Document, on the Ryzen). Since 0.31, three allocations of each match are left, those of its object; with mimalloc, $match took 0.38 ms, so the gap left is the Windows heap.

What the limits cost

Every evaluation counts its steps for the step limit (ADR 0018). Without a limit the budget is too large to be spent, so a step costs the same comparison with or without one. The timeout reads the clock at every 256th step rather than at every step, so a timeout fires at most 256 steps late. On a loop of 300,000 small steps, a timeout that never expires costs 0.3% natively and 2.4% in WebAssembly, where the clock is a call out of the module, and a step limit that is never reached at most 1.1% (measured for 0.20). A regular expression under a timeout or a cancellation token checks it every 1,024 steps of the backtracker (ADR 0009), the engine’s interrupt.

Using the API efficiently

  • Compile once. Expression::compile (new Expression in JavaScript) parses and post-processes. A compiled expression is immutable: keep it, share it between threads (it is Send + Sync), and evaluate it as often as needed.
  • Reuse documents. evaluate converts the parts of the input that the expression reads. For an expression that reads all of a 24 KB document, that costs about 40 µs natively, and about 160 µs in WebAssembly. To evaluate several expressions against one document, such as a set of rules, or one expression repeatedly, convert the document once.
  • Prefer JSON text when you have it. Document::parse (evaluateJson in JavaScript) parses straight into jlens’s representation. That is faster than parsing into a serde_json::Value and converting it.
  • Audit at the level you need. Without a tracer, auditing costs nothing measurable; see ADR 0004. Level decisions records only what explains a result. full records every node, with its input, and is for debugging.

Reusing documents

#![allow(unused)]
fn main() {
use jlens_core::{Bindings, Document, Expression, Options};

let doc = Document::parse(r#"{"items": [{"price": 2}, {"price": 3}]}"#)?;
let rules: Vec<Expression> = ["$sum(items.price) > 4", "$count(items) < 10"]
    .iter()
    .map(|source| Expression::compile(source))
    .collect::<Result<_, _>>()?;
for rule in &rules {
    let outcome = rule.evaluate_document(&doc, &Bindings::new(), &Options::default())?;
}
Ok::<(), Box<dyn std::error::Error>>(())
}
const doc = new Document(input);              // or Document.fromJson(text)
for (const rule of rules) rule.evaluateDocument(doc);
doc.free();

Evaluations never modify a document, so one document serves any number of evaluations. A Document is not Send. Convert it on the thread that uses it.

Document::parse_cut parses JSON text without the fields that the rules do not read, given what they read (ADR 0016): one rule’s Reads::input, or the union of the rules’. It checks the syntax of the fields it skips but does not convert them, so the narrower the reads, the faster the parse:

ParseDocument::parseDocument::parse_cut
The 24 KB document, cut by what names over items reads65 µs37 µs
The same, cut by the union of every case (the transform reads it whole)65 µs65 µs
1,000 records of 40 fields (1 MB), cut by what $sum(records[status = 'open'].amount) reads3.18 ms1.19 ms

A rule that reads a document whole leaves nothing to skip, and then parse_cut costs what parse does.

The WebAssembly and Python packages parse this way: Document.fromJson and Document.from_json take a Read, and evaluateJson and evaluate_json cut their input by what the expression reads. On the Ryzen, in Python, 1,000 records of 40 fields (790 KB) parse in 5.5 ms whole and 1.6 ms cut by the rule above, and evaluate_json evaluates it on them in 1.6 ms in all; in Node.js, evaluateJson evaluates the rule on 1,000 such records (1.4 MB) in 5.7 ms.

jlens eval parses its input this way too, by what the expression reads. Measured end to end on the Ryzen, process start included, before and after:

jlens evalParsed wholeParsed cut
$sum(items.price) on 30,000 items of 5 fields (10 MB)76 ms27 ms
$count(items) on the same73 ms21 ms
--ndjson --filter '$sum(items.price) > 470' on 20,000 lines of 5 items (34 MB)175 ms95 ms
$length($string($)) on the 10 MB document, which reads it whole159 ms107 ms

The last row gains too: the input used to be parsed into a serde_json::Value and converted, where it is now parsed straight into a Document.

Documents built from Rust values

An application that keeps its state in Rust types builds its document with Document::from_serialize, which serializes the state straight into it (ADR 0019), rather than through a serde_json::Value. The benchmark host_state (cargo bench -p jlens-core --bench host_state) measures a state of ten records of twenty fields (strings, numbers, booleans, small lists), rebuilt at each iteration:

host build: building the documentTime
serde_json::to_value alone, for reference9.3 µs
to_value + Document::new19.4 µs
serde_json::to_string + Document::parse18.6 µs
Document::from_serialize9.3 µs

host rebuild builds the document and evaluates the first 1, 4 or 10 of ten small expressions (filters, sorts, [0]) against it, as an application does between two changes of its state. For comparison, the last two rows build no document: lazy evaluate over the serde_json::Value, and evaluate_source over a Source written over the state, which reads only the fields each expression reaches:

host rebuild1410
to_value + Document::new + evaluate_document20.1 µs23.4 µs32.2 µs
Document::from_serialize + evaluate_document10.2 µs13.4 µs22.1 µs
to_value + evaluate10.9 µs16.7 µs29.9 µs
evaluate_source1.6 µs7.5 µs22.2 µs

For up to four evaluations per state, a Source is the fastest; at ten, a document built with from_serialize is level with it.

host binding reads one of the ten records through a variable ($self.score > 50 and $self.active). A variable of Bindings is a serde_json::Value, of which each evaluation converts what the expression reads; evaluate_document_with binds the record’s part of the document (Document::pointer("/items/3")), which is not converted:

host bindingTime
$self from Bindings0.31 µs
$self bound to doc.pointer("/items/3")0.15 µs
items[3] read from the document, for reference0.30 µs

Variables against a Document

An evaluation against a Document cannot read a variable lazily, as evaluate_with does: a lazy value borrows the Bindings, and a document’s values are 'static (ADR 0019). Since 0.27 it converts what reads() says the expression reads of each variable, analysed once for each expression; before, it converted each variable whole.

A variable of 1,000 records of seven fields, about 130 KB, against a document of one field, on the Ryzen, with functions and blocks aligned, in µs, the fastest of 15 rounds of one process:

ExpressionBefore 0.270.27evaluate_with
$v.total1,0500.640.57
$v.owner.name1,0500.990.92
$v.items[3].price1,0502331.77
$sum($v.items.price)1,09026087
a + 1, the variable not read1,0500.210.59
$count($keys($v)), read whole1,0501,0582.2
$self.hp > 10, five fields1.550.680.52
a > 10, five fields not read1.510.210.49
$x + a, a number0.3150.3280.41
a + 1, no variables0.1990.2020.30

What is still slower than a lazy read is an array read for a few of its items: $v.items[3].price converts price of all 1,000 items. A variable read whole costs what it did. To read a large variable for a few of its parts, bind a part of a Document instead (evaluate_document_with and Document::pointer, above), which converts nothing. The Python and Java packages evaluate this way too: from Java, $v.total takes 0.83 µs where it took 630. From 0.35 the Java package binds a Document to a variable as well (evaluate(input, Map.of("table", document))): on the M1, $table.items[0].name over 100,000 items takes 0.8 µs, and 8.3 ms with the variable in Bindings.

Where the time goes

  • Allocation. Values are reference-counted (Rc). Objects keep their fields in insertion order, in a vector searched in order; one of more than 8 fields also keeps an index of its keys (ADR 0022). Their keys are shared strings: converting a document allocates each distinct key once, not once per object. Building results, sequences and tuple streams allocates, and that is most of the cost of evaluation.
  • Sequences. Following jsonata-js, paths build sequences step by step and flatten them. Predicates evaluate per item, in a fresh frame. A step that reads an array passes it on as it is, and a step whose results hold no array to flatten makes them its sequence without copying.
  • Shortcuts for common shapes. Without a tracer, evaluation skips work that no result depends on. A path of names on a single object (customer.name, $i.code) looks its fields up instead of building sequences; a name mapped over the items of a path is looked up in each item; a predicate built from literals and variables ([[0..19]], [$i]) is evaluated once and picks its items; an object constructor over one item evaluates its pairs without grouping; = with a string literal compares with the literal’s text. With a tracer, evaluation takes the general path, so audits report every node; a property test checks that both paths agree.
  • Converting the input. evaluate converts the parts of the input that the expression reads, as it reads them (ADR 0010): a path step reads only the field it looks up, and an index only the items it picks. Through a Source, as the Python and Java packages read their values, each array or object read costs one allocation, and freeing it as much again. An expression that reads whole items pays for converting them.
  • Fixed cost. An evaluation reads the clock only when it needs it ($now, $millis, the seed of $random, a timeout), so customer.name against a Document takes 0.10 µs natively. From Python a call adds about 0.25 µs (its arguments, the interpreter released during the evaluation, the result built); from Java the C API and the call add about 0.12 µs, and the Ryzen’s 16 threads on one Document made 20.8 million evaluations of customer.name per second.
  • Calls of lambdas. A call of a lambda takes its frame and its arguments’ list from those that a call before it gave back, and allocates them only when none is free or something still holds them, a closure for one; a name of up to 14 bytes stays in the frame’s entry. A variable read in a lambda’s body knows which parameter holds it. Host functions are looked up on a miss, so that binding sixteen of them costs nothing per evaluation (ADR 0006).
  • Deep copies. A transform (|...|) copies its input with $clone before changing it, as jsonata-js does; jlens copies the value itself, with the values a copy through JSON text would give, where jsonata-js writes and parses the text. On the benchmark’s transform the copy takes about a fifth of the time; most of the rest is building the result.
  • Long inputs. $distinct keeps its first 16 distinct items by comparing, and looks the rest up by a hash, of the first values comparing reads when they tell the items apart, following them into the level below a field the items share: on the Ryzen, 100,000 strings take 15 ms, where jsonata-js, quadratic, takes 32 s. & in a loop holds its two sides and makes its text once, as V8’s ropes do. $substring walks the text only as far as its result. What cargo xtask growth still finds quadratic is so in any engine, as each step copies what it has built: $reduce that builds an array with $append or an object with $merge, and in over a long list for each item. An expression that does this over large input can be bounded with max_steps or max_array_length.
  • Link-time optimization. Every release build uses LTO, the C API and the WebAssembly module since 0.27 (2 to 24% faster). Its larger frames make a deep recursion overflow V8’s stack 4 to 9% sooner; the default maxDepth of 500 keeps a third of headroom (WebAssembly guide).

0.37 against 0.36.0

Measured for the release (roadmap 0.37 #16) on the M1, against builds of 0.36.0 made the same way, each run on a new copy of its build; the full comparison is in docs/notes/perf-0.37.md. 0.37 compiles a regex literal once per compiled expression, checks the size limits while a result is built, and finds a Java thread’s conversion of a document by its handle.

regex takes 11 to 15% less natively, 10 to 15% less in Python, 7 to 14% less in Java and 8 to 18% less in WebAssembly. $toMillis of an ISO 8601 timestamp, with no picture, takes 88% less (200 calls in 0.22 ms, 1.9 ms in 0.36.0), as its pattern is compiled once for the process. path against a Document takes 6% less natively and 7% less in Java. Natively every other case is within 3% in each mode. Python and Java slow no case past their marks (3.5% and 5%); Java’s one mark, filter + aggregate over Java objects, 9% slower in one comparison, is a draw of the JIT, as the table’s run of the same build gives 0.36.0’s time. In WebAssembly, regex over a long text takes 4% more (177 to 184 µs), in two steps of about 2% that native builds do not show: the size limits checked as $replace builds its text (#5), and the commits ported to jlens-regress (#20). Every other case is within 2.1%, but times under 4 µs, printed to a tenth.

Compiling customer.name reads 0.64 µs where 0.36.0’s table read 0.59 µs; built and run one after the other, 0.36.0 takes 0.61 µs and 0.37 0.60 µs, so the difference is between builds, not in the code. On its own benchmark, the engine is within 2% of 0.36.0 on every case but two faster (Unicode text 3%, anchored, short text 5%), and the interrupt costs 2.8% where it cost 2.4%.