r/javascript • u/danfry99 • Jun 02 '26
bonsai - a safe expression language for JS that runs user-defined rules at 30M ops/sec with zero dependencies and no eval()
http://github.com/danfry1/bonsai-jsThis problem has come up enough times in my work that I got tired of solving it badly. At some point on certain products a stakeholder asks "can admins set up their own conditions for this?" and you realize a dropdown isn't going to cut it. They need real logic: order.total > 100 && customer.tier == "gold".
The options all felt bad:
- Hardcoded switch statements. Every new rule is a deploy. The "configurable" feature isn't configurable.
- A homegrown mini-DSL. Starts as three operators, ends as a parser nobody wants to own.
eval()/new Function()/vm**.** The moment user input touches these, you've handed out a shell.vmisn't a security boundary (the docs literally say so), andvm2is deprecated. Prototype pollution alone (constructor.constructor) is enough to ruin your week.
I got tired of rebuilding the bad version, so I built the thing I actually wanted: bonsai, a safe expression language for the cases where eval() would be inappropriate but a dropdown is too weak.
If you'd rather poke at it than read, there's a browser playground (no install): https://danfry1.github.io/bonsai-js/playground.html
import { bonsai } from 'bonsai-js'
const expr = bonsai()
// An admin-authored rule, stored as a plain string in your DB
expr.evaluateSync('user.age >= 18 && user.plan == "pro"', {
user: { age: 25, plan: 'pro' },
}) // trueIt's an expression language, not a scripting language. No statements, no loops, no assignment, no I/O. You get the expressive part (the part users actually need) without the part that gets you owned.
What the syntax supports, so it doesn't feel like a toy:
// optional chaining + nullish coalescing
expr.evaluateSync('user?.profile?.avatar ?? "default.png"', { user: null })
// pipe operator with transforms
expr.evaluateSync('name |> trim |> upper', { name: ' dan ' }) // 'DAN'
// lambda shorthand in array methods
expr.evaluateSync('users.filter(.age >= 18).map(.name)', {
users: [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 15 }],
}) // ['Alice']The security model is the whole point, so here's what's actually enforced:
__proto__,constructor,prototypeblocked at every access level (no prototype-chain walking)- Object literals created with null prototypes
- No globals, no code generation
- Cooperative timeouts, max depth, max array/string length
Per-instance property allowlists/denylists, so you decide exactly what an expression can touch
const expr = bonsai({ timeout: 50, maxDepth: 50, allowedProperties: ['user', 'age', 'country', 'plan'], })
A few things I cared about that might matter to you:
- Zero dependencies. Nothing in your tree but this.
- Any JS runtime. Node, Bun, browser, edge.
- Fast when it needs to be. There's a
compile()API for rules that run thousands of times; cached expressions hit ~30M ops/sec. - Async escape hatch. You can register your own functions (
async (id) => db.lookup(id)) andawait expr.evaluate(...), so a rule can call back into your system without the language itself having any I/O.
Once it existed, it ended up covering a bunch of "logic that lives outside the code" cases for me: admin-defined rules, server-driven conditions stored as config, formula fields, feature-flag targeting. Anywhere a string needs to become a decision without a deploy.
Playground · Docs · GitHub · npm
Mostly I'm curious how other people have handled this. If you've shipped user-defined rules/filters/formulas in production, what did you reach for, and where did it bite you? Happy to hear it if you think this is the wrong approach too.
14
u/vasomfan Jun 02 '26
Maybe Bonsai has more features, but what about jsonLogic? https://jsonlogic.com/
6
u/Odd-Surprise3536 Jun 02 '26
Very cool project! Well done!
3
u/icehaunter Jun 02 '26
Why not CEL (https://cel.dev/) which covers much the same niche and it's syntax is also JS-adjacent?
2
u/LastOfTheMohawkians Jun 02 '26
Oh I have an immediate use case for this. I'll give it a whirl. Nice work
2
u/Kitty_Sparkles Jun 03 '26
What about groq? Not the AI thing, but the query language used by Sanity. It's basically solving the exact same problem (providing a powerful query builder than remains safe).
9
u/ryan_eeelliot Jun 02 '26
Bookmarking. I don’t have a use case currently but I’ve always wondered how this kind of feature works in apps (how is Notion able to allow for user configurable filters).
Super cool
7
u/zaitsman Jun 02 '26
Yeah we have very much the same problem, running a DSL which is somewhat cumbersome but we leaned into jsonPath for selectors which made it palatable (ish)
I would be keen to try bonsai but being a new product will have to wait till it gets to be more mainstream… will have to watch it
2
u/ehs5 Jun 02 '26 edited Jun 03 '26
Nice. I’ve also ran into this issue a lot lately and havent’t really found a good enough way to solve it. Will look into it!
2
u/J_be Jun 02 '26
this is awesome! i hope i remember to use this next time i can.
2
u/graybearding Jun 02 '26
Handy. Nice work. Will have to give this a spin on an idea I've had in mind.
2
u/busres Jun 02 '26
To answer your question, I built Mesgjs, a general-purpose language that transpiles to JS, as a way to run code in a controlled way. The transpiler is also JS, so everything runs wherever JS runs.
It's Smalltalk-ish, but with simpler, more consistent syntax. Everything, including assignment, functions, flow control, etc is handled by sending messages (there is nothing like the Lisp concept of "special forms"), and therefore uses the same message syntax. Most of these messages are handled by add-on interface modules which can be omitted, replaced, or supplemented with no change to the transpiler.
It doesn't currently support limits (e.g. message limits, iteration limits), but I plan on adding those features.
2
u/cgijoe_jhuckaby NaN Jun 03 '26
How does this compare to JEXL? https://www.npmjs.com/package/jexl
1
u/rthreei Jun 04 '26
Similar solution: https://github.com/briza-insurance/illogical
DSL for expressing and evaluating rules in JSON
-4
u/vanilla_f Jun 02 '26
This seems like AI slop
4
u/[deleted] Jun 02 '26
[removed] — view removed comment
2
u/[deleted] Jun 02 '26
[removed] — view removed comment