Open sprappcom opened 4 weeks ago
Hello,
Let me explain the difference.
It may seem what expr and goja solves a similar problem, but they do it in a little bit different way. Also use cases can be different, goja targets more environments where JS may be needed, Expr on the other hand targets rule engines, filtering, and policy of different kinds.
A few differences between goja and expr:
Main difference is that Expr can be type checked. Expr comes with advance type checker which can use type defined directly in Go program. This is very useful, for example, if you want users to allow editing expressions in UI and on save button your program can type check the new expression and warn of any errors found.
goja/otto implement full JS, so you need to be aware of what users can write: infinite loops while(true);
, self-recursions, an so on. Expr is non-Turing complete and has limited set of functionality available for users. Expr also has more expressive syntactic sugar, for example predicates all(tweets, .size <= 280)
. Also, in Expr you have full control other the language and what features available to users.
Patching. Another powerful tool in Expr is AST patching (expr-lang.org/docs/patch). Patching allows to modify expressions in any possible form before compilation. A lot of companies use this powerful feature in a lot of different ways.
Expr designed to be working with Go. Expr does not wrap values and uses them directly. As goja is JS vm, goja has to wrap all passed values into Value
struct (sometimes with strange consequences) and unwrap ExportTo
.
Speed. As Expr was designed specifically for Go and with business rule engines/expressions in mind, Expr is faster than goja/otto (bench). Expr uses own virtual machine, Expr has optimizing compiler, and Expr uses a few tweaks to speed up the Expr/Go communications.
Expr is dependencies free. Check out Expr's go.mod file. Expr has zero dependencies; this makes auditing process is much easier, easier to upgrade.
Both Expr and goja a very cool and powerful languages. And you need to choose what suits you best for the task you are working on.
thx so much for the comprehensive explanation.
just curious, i was looking at making my own cloudflare workers (which would prefer js than wasm), so was wondering if i can use expr as replacement?
i know expr is not using js but i was wondering will it be a good fit for FaaS kind of scenario,
also i'm hoping able to do "hot reload" like detecting file changes and reload expr lang script. <- any caveats? for example i wont use "golang plugin" reload as memory is not reclaimed for the old unused golang plugins.
will expr be a good fit for FaaS? what caveats do i have to look out for?
@antonmedv appreciate this last question. thx. will use expr in filtering too.
as titled