Repository navigation
Compile if with fewer than three arguments - #61
Merged
Merged
Conversation
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.
if with fewer than three arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
if.compilereturnsfalsewhen it gets fewer than three arguments. A built rule with{"if": [cond, then]}therefore calls the interpretedif.methodat run time. That method copies the arguments and runs each branch throughrunOptimizedOrFallback. JsonLogic reads a two-argumentifas[cond, then, null], andif.methodalready answers it that way.This PR makes
if.compileemit the same answers asif.methodfor short argument lists:engine.truthy(cond) ? then : null. The existing loop already adds anullelse for an even length, so only the guard changes.null.Three or more arguments compile as before. A non-array argument still returns
false, so{"if": "apple"}still throws fromif.method.The motivating user is
@openfeature/flagd-core, flagd's JavaScript evaluator. Its targeting rules often use a two-argumentif, for example{"if": [{"and": [...]}, {"fractional": [...]}]}for a percentage rollout. In a profile of such rules, the interpretediftook about 11 % of evaluation time on top of the work inside its branches.Changes
defaultMethods.js:if.compilehandles 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 withfalse,0,"",[],nulland a missing value as the condition, athrowin the then branch under a false condition, and a nested two-argumentif. The conditions come fromval, because the default engine evaluates a constant rule at build time and never callsif.compilefor it. The existing{"if": []}case covers zero arguments in the engine modes with inlining disabled.Verified
masterbefore 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.jsis clean.engine.methods.if.methodandasyncMethodwith functions that throw, then built{"if": []},{"if": [{"var": "x"}]}and{"if": [{"var": "x"}, "apple"]}withLogicEngineandAsyncLogicEngine, inlining disabled. Onmasterevery 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
ifon the defaultLogicEngine, Node 26.13.0, Apple M1 Pro. Each number is ns per run, the median of three processes that alternated betweenmasterand this branch, each taking the median of 7 rounds of 2,000,000 calls after warm-up.{"if": [{"var": "x"}, {"var": "y"}]}xtrue{"if": [{"var": "x"}, {"var": "y"}]}xfalse{"if": [{"==": [{"var": "tier"}, "gold"]}, "on"]}tieris"gold"{"if": [{"==": [{"var": "tier"}, "gold"]}, "on"]}tieris"free"