I need to evaluate a boolean expression built by the user. I see two ways to do it:
1) build a string and pass it off to eval
2) build an abstract syntax tree and evaluate it myself
I have implemented it with eval since it’s the simplest solution. I’m not convinced that the second option is going to be much better, i wouldn’t be surprised if it was actually slower.
Security shouldn’t be an issue since i’m building the expression from a set of predetermined values.
In most cases the expressions are going to be pretty simple, but it’s possible to build more complex expressions. This is about as complex as it’s going to get: “(true||false&&false)&&(true)”
Thoughts?
I think this is a valid usecase for eval. However, if you were to ever want to use CSP (content security policy), which is awesome.. eval may be disabled.