Skip to content

Compile in with a long literal list to a Set lookup - #60

Open
bmmass wants to merge 1 commit into
json-logic:masterfrom
bmmass:perf/in-literal-list
Open

bmmass wants to merge 1 commit into
json-logic:masterfrom
bmmass:perf/in-literal-list

Conversation

@bmmass

@bmmass bmmass commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A compiled {"in": [x, [ ...literals ]]} writes the list into the generated function as an array literal, so each run builds the array and scans it with includes. For long lists, such as an allow list of a few hundred user ids, as feature-flag rules often have, the scan is most of the rule's cost.

When the list has more than 256 items and every item is a string, number, boolean or null, in.compile now builds a Set once at compile time, keeps it in values, and emits values[n].has(x). Set#has and Array#includes both compare by SameValueZero, so the answers do not change. Shorter lists and the other in forms (a list with logic inside it, a string, a var) compile as before.

Numbers

Compiled {"in": [{"var": "x"}, ["id-0", "id-1", ...]]} with a value that is not in the list, rule parsed from JSON, Apple silicon, median ns per run. "New string" builds "user-" + i before each call, so the engine never saw it before. "Hashed string" cycles through 1,000 strings, which matches one context evaluated against many rules.

New string:

List length Node 22 before Node 22 after Node 24 before Node 24 after Node 26 before Node 26 after
256 237 236 236 236 237 237
257 238 62 242 66 237 233
400 349 69 347 71 349 241
1000 816 70 818 73 817 254

Hashed string:

List length Node 22 before Node 22 after Node 24 before Node 24 after Node 26 before Node 26 after
256 223 227 223 223 222 223
257 223 22 233 21 224 22
400 334 25 346 23 335 24
1000 805 25 833 24 803 25

Tests

  • suites/in-literal-list.json adds 18 cases for a 5-item list and a 263-item list: hits, misses, "1" against 1, null, booleans, a missing variable and an array value. The compatible suite runs them in all 8 engine modes, interpreted and built (288 tests). They pass before and after the change.
  • yarn test: 18,966 passed. eslint defaultMethods.js is clean.
  • A separate script compared built and interpreted answers, sync and async, for a 276-item list holding NaN, -0 and mixed types. It checked NaN, -0, 0, {}, [], [1], "1" against 1, "true" against true, a missing variable, hits and misses. All answers matched.

Questions you may have

Why a cutoff of 256?
Set#has hashes a string the first time it sees it, and V8 caches that hash on the string. On Node 22 and 24, a lookup with a string the engine has never hashed costs about 65 ns at any list length. On Node 26.12 and 26.13 it costs about 205 to 255 ns, the same as scanning about 256 short strings. With 256 as the cutoff, no case in the tables above is slower than master on Node 22, 24 or 26.

Is the Node 26 cost a V8 regression that will go away?
I don't know the cause. It shows on Node 26.12.0 and 26.13.0, and not on 22.21.1 or 24.22.2. If a later Node 26 brings it back to about 65 ns, the cutoff can drop to about 64. That is where a Set starts to win on Node 22 and 24.

Could lists of only numbers use a lower cutoff?
Yes. Numbers are cheap to hash, so a Set matches the array at about 64 numbers on both Node 22 and 26. At 256 numbers, the lookup drops from 104 ns to 31 ns on Node 26. I kept one cutoff to keep the change small, and I can add a number-only cutoff of 64 if you want it.

Is it safe to share one Set across runs?
Yes. The Set lives in the built function's values, and the generated code only calls has on it. It never passes the Set to a method, so a custom method cannot reach or change it. Only the boolean result leaves. The Set also takes a snapshot of the list at build time, like the JSON text the generated code held before. Changing the rule object after build has no effect either way.

What does it cost at compile time and in memory?
Building a rule with a 1000-item string list is faster than before: 123 µs against 348 µs on Node 26, and 84 µs against 430 µs on Node 22. The generated source no longer holds the list as text, which more than pays for the every check and the Set build. Each built function keeps about 6 KiB more heap for a 1000-item list: 20.5 KiB against 14.1 KiB.

Why are short lists left alone?
I tried building short lists once as arrays too. It saved only 1 to 5 ns per run. It also made rules built in code about 5 times slower when their ids came from template strings. V8 stores those as rope strings, and includes compares rope strings slowly. Fixing that needed a copy of every string at compile time, which was not worth the gain.

A compiled `in` writes its list into the generated function, so each run
builds the array and scans it. When the list has more than 256 strings,
numbers, booleans or nulls, build a Set once at compile time and call
`has`. Set#has and Array#includes both compare by SameValueZero.

256 is where hashing a new string on Node 26 costs about as much as the
scan, so shorter lists compile as before.
@TotalTechGeek

Copy link
Copy Markdown
Member

I'll probably merge this -- the cost of maintenance is negligible, I think my one "hang up" (and it's tiny) is when I explored this before, my thought process was:

"Well, I think it'd be nice if it were optimized to use Set, but how common is the use-case where MANY items are statically baked into the rule, rather than coming from a dynamic value? Also, should I modify in so that it also works natively on a Set?"

@bmmass

bmmass commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look.

On how common it is: flag configs are the case I had in mind. flagd's JavaScript evaluator (@openfeature/flagd-core) uses json-logic-engine 5.0.7. In flagd the targeting rule is part of the flag definition and the user id comes from the evaluation context, so an allow list of named users or emails sits in the rule as literals. Those lists grow to a few hundred entries when a team rolls a feature out to named users. Lists that come from data stay on the existing includes path, so this PR does not change them.

On in with a Set: today a Set from data throws in both modes ((array || []).includes is not a function), so supporting it would not break anyone. A check like array instanceof Set ? array.has(item) : (array || []).includes(item) would cover the dynamic case for callers who build their context in JavaScript, since JSON data cannot carry a Set. I'd keep that as a separate PR, because it changes what in accepts rather than how it compiles. Happy to open it if you want it.

CI is waiting for workflow approval, since this is my first PR here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants