Skip to content

Compile if with fewer than three arguments - #61

Merged
TotalTechGeek merged 1 commit into
json-logic:masterfrom
bmmass:perf/compile-short-if
Nov 15, 2026
Merged

TotalTechGeek merged 1 commit into
json-logic:masterfrom
bmmass:perf/compile-short-if

Conversation

@bmmass

@bmmass bmmass commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Summary

if.compile returns false when it gets fewer than three arguments. A built rule with {"if": [cond, then]} therefore calls the interpreted if.method at run time. That method copies the arguments and runs each branch through runOptimizedOrFallback. JsonLogic reads a two-argument if as [cond, then, null], and if.method already answers it that way.

This PR makes if.compile emit the same answers as if.method for short argument lists:

  • Two arguments compile to engine.truthy(cond) ? then : null. The existing loop already adds a null else for an even length, so only the guard changes.
  • One argument compiles to that argument's value.
  • Zero arguments compile to null.

Three or more arguments compile as before. A non-array argument still returns false, so {"if": "apple"} still throws from if.method.

The motivating user is @openfeature/flagd-core, flagd's JavaScript evaluator. Its targeting rules often use a two-argument if, for example {"if": [{"and": [...]}, {"fractional": [...]}]} for a percentage rollout. In a profile of such rules, the interpreted if took about 11 % of evaluation time on top of the work inside its branches.

Changes

  • defaultMethods.js: if.compile handles zero, one and two arguments.
  • suites/control/if.json: 15 cases under "Fewer than three arguments, from data". They cover one argument (a value, a falsy value, a missing value), two arguments with truthy conditions, two arguments with false, 0, "", [], null and a missing value as the condition, a throw in the then branch under a false condition, and a nested two-argument if. The conditions come from val, because the default engine evaluates a constant rule at build time and never calls if.compile for it. The existing {"if": []} case covers zero arguments in the engine modes with inlining disabled.

Verified

  • The new cases pass on master before the change and after it. The compatible suite runs each one in all 8 engine modes, interpreted and built.
  • yarn test: 18,918 passed, before and after.
  • eslint defaultMethods.js is clean.
  • A scratch script replaced engine.methods.if.method and asyncMethod with functions that throw, then built {"if": []}, {"if": [{"var": "x"}]} and {"if": [{"var": "x"}, "apple"]} with LogicEngine and AsyncLogicEngine, inlining disabled. On master every build fell back to the method and threw. On this branch every build ran without it and returned the expected value.

Benchmark of a built two-argument if on the default LogicEngine, Node 26.13.0, Apple M1 Pro. Each number is ns per run, the median of three processes that alternated between master and this branch, each taking the median of 7 rounds of 2,000,000 calls after warm-up.

Rule Data master this PR
{"if": [{"var": "x"}, {"var": "y"}]} x true 111 5.5
{"if": [{"var": "x"}, {"var": "y"}]} x false 86 3.4
{"if": [{"==": [{"var": "tier"}, "gold"]}, "on"]} tier is "gold" 87 6.0
{"if": [{"==": [{"var": "tier"}, "gold"]}, "on"]} tier is "free" 89 6.5

if.compile returned false for fewer than three arguments, so a built
rule fell back to the interpreted if.method for {"if": [cond, then]}.
The compiler now emits what if.method returns. Two arguments go
through the existing loop, which already adds a null else for an even
length. One argument compiles to its value, and zero arguments compile
to null.

The new cases in suites/control/if.json read the condition from data,
so the default engine builds them instead of evaluating them up front.
@bmmass bmmass changed the title Compile if with fewer than three arguments Compile if with fewer than three arguments Nov 15, 2026
@TotalTechGeek
TotalTechGeek merged commit b101b8c into json-logic:master Nov 15, 2026
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