Repository navigation
Conversation
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.
|
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 |
|
Thanks for taking a look. On how common it is: flag configs are the case I had in mind. flagd's JavaScript evaluator ( On CI is waiting for workflow approval, since this is my first PR here. |
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 withincludes. 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.compilenow builds aSetonce at compile time, keeps it invalues, and emitsvalues[n].has(x).Set#hasandArray#includesboth compare by SameValueZero, so the answers do not change. Shorter lists and the otherinforms (a list with logic inside it, a string, avar) 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-" + ibefore 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:
Hashed string:
Tests
suites/in-literal-list.jsonadds 18 cases for a 5-item list and a 263-item list: hits, misses,"1"against1,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.jsis clean.NaN,-0and mixed types. It checkedNaN,-0,0,{},[],[1],"1"against1,"true"againsttrue, a missing variable, hits and misses. All answers matched.Questions you may have
Why a cutoff of 256?
Set#hashashes 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
Setstarts to win on Node 22 and 24.Could lists of only numbers use a lower cutoff?
Yes. Numbers are cheap to hash, so a
Setmatches 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
Setacross runs?Yes. The
Setlives in the built function'svalues, and the generated code only callshason it. It never passes theSetto a method, so a custom method cannot reach or change it. Only the boolean result leaves. TheSetalso takes a snapshot of the list at build time, like the JSON text the generated code held before. Changing the rule object afterbuildhas 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
everycheck and theSetbuild. 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
includescompares rope strings slowly. Fixing that needed a copy of every string at compile time, which was not worth the gain.