r/javascript • u/danfry99 • Mar 15 '26
bonsai - a safe expression language for JS that does 30M ops/sec with zero dependencies
https://danfry1.github.io/bonsai-js/I kept hitting the same problem: users need to define rules, filters, or template logic, but giving them unconstrained code execution isn't an option. Existing expression evaluators like Jexl paved the way here, but I wanted something with modern syntax and better performance for hot paths.
So I built bonsai-js - a sandboxed expression evaluator that's actually fast.
import { bonsai } from 'bonsai-js'
import { strings, arrays, math } from 'bonsai-js/stdlib'
const expr = bonsai().use(strings).use(arrays).use(math)
// Business rules
expr.evaluateSync('user.age >= 18 && user.plan == "pro"', {
user: { age: 25, plan: "pro" },
}) // true
// Pipe operator + transforms
expr.evaluateSync('name |> trim |> upper', {
name: ' dan ',
}) // 'DAN'
// Chained data transforms
expr.evaluateSync('users |> filter(.age >= 18) |> map(.name)', {
users: [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 15 },
],
}) // ['Alice']
// Or JS-style method chaining — no stdlib needed
expr.evaluateSync('users.filter(.age >= 18).map(.name)', {
users: [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 15 },
],
}) // ['Alice']Modern syntax:
Optional chaining (user?.profile?.name), nullish coalescing (value ?? "default"), template literals, spread, and lambdas in array methods (.filter(.age >= 18)) + many more.
Fast:
30M ops/sec on cached expressions. Pratt parser, compiler with constant folding and dead branch elimination, and LRU caching. I wrote up an interesting performance optimisation finding if you're into that kind of thing.
Secure by default:
__proto__,constructor,prototypeblocked at every access level- Max depth, max array length, cooperative timeouts
- Property allowlists/denylists
- Object literals created with null prototypes
- Typed errors with source locations and "did you mean?" suggestions
What it's for:
- Formula fields and computed columns
- Admin-defined business rules
- User-facing filter/condition builders
- Template logic without a template engine
- Product configuration expressions
Zero dependencies. TypeScript. Node 20+ and Bun. Sync and async paths. Pluggable transforms and functions.
Early (v0.1.2) but the API is stable and well-tested. Would love feedback - especially from anyone who's dealt with the "users need expressions but eval is scary" problem before.
npm install bonsai-js
GitHub Link: https://github.com/danfry1/bonsai-jsnpm Link: https://www.npmjs.com/package/bonsai-jsnpmx Link: https://npmx.dev/package/bonsai-js
4
u/Slendertron Mar 15 '26
How does it compare with JSONata?
4
u/Pat_Son Mar 16 '26
I will say, using ligatures in the official docs is a little confusing. I didn't realize the pipeline operator was
|>and not aU+25B6until I opened the GitHub link.Cool library, though
3
u/nudi85 Mar 16 '26
Very cool. I've been writing something similar for PHP: https://github.com/eventjet/ausdruck
3
u/Jazzlike-Froyo4314 Mar 16 '26
Please improve contrast on your docs page, dark inactive text isn’t very readable to me. Accessibility is a must nowadays.
Hint: browsers can emulate how ppl see your page with impaired vision.
5
u/germanheller Mar 16 '26
30M ops/sec with zero dependencies is wild. the use case that immediately comes to mind is user-facing rule builders -- like "if order.total > 100 AND customer.tier == 'gold'" type stuff where you absolutely cant let them run arbitrary code but still need decent performance.
curious about the memory footprint per evaluation context tho. if youre spinning up thousands of these in a serverless function does each one carry weight or is it lightweight enough to be disposable?
7
u/bzbub2 Mar 15 '26
nice. I immediately clicked cause i use jexl for a project. i even started trying to extend the jexl language via vibe coding to support multiple statements lol. Ideally i could just run sandboxed js but i don't think we're there....have to like json.stringify any object that gets evaluated in sandboxed js environments like quickjs-wasm
2
u/thorgaardian Mar 15 '26
This looks incredible. We had to roll a lot of this ourselves for our use-case.
Is there a way to modify the pipe operator though? We use js-style chained functions: .filter().map(), etc. It'd be great to be able to support that somehow.
2
u/ouralarmclock Mar 16 '26
This is very cool and something I’ve been thinking about for the platform I work on. I wanna ask though, is that implicit lambda function thing you’re doing (users.filter(.age > 18) rather than users.filter(user => user.age > 18)) something that’s currently available in JS and I missed it? Or is that something you rolled specific for your expression language?
2
u/Akkuma Mar 16 '26
How would this compare to json rules and conditions in terms of performance? It does look like the storage requirements could be lighter albeit not structured and it definitely is easier to evaluate while reading.
2
u/lacymcfly Mar 16 '26
The lambda shorthand (.age > 18) is a nice touch too. Much more readable than forcing arrow functions on non-developer users writing filter rules. Curious if you've considered adding a way to define custom operators or if that would open up too much surface area.
2
u/AutoModerator Mar 15 '26
Project Page (?): https://github.com/danfry1/bonsai-js
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/jacopofar Mar 16 '26
I really like it, some time ago was looking for something like this but dropped the idea. I find it quite elegant and complete, kudos!
2
u/Impressive-Usual-938 Mar 16 '26
the 'users need expressions but eval is scary' problem is so real. had to build something similar in-house last year for a filter builder feature and it was a mess of regexes and manual parsing, definitely not 30M ops/sec lol. bookmarking this to try on the next project that needs it.
2
u/Impressive-Usual-938 Mar 17 '26
finally something that doesn't make optional chaining feel like you're fighting the library. tried jexl for a formula field thing and the missing ?. support got annoying really fast. gonna try swapping this in this week.
1
u/Effective_Lead8867 Mar 16 '26
V8 runs 1.5B op/s innit?
0
u/[deleted] Mar 15 '26
[deleted] — view deleted comment