Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile and evaluate separately #30

Open
markhats opened this issue Apr 16, 2020 · 3 comments
Open

Compile and evaluate separately #30

markhats opened this issue Apr 16, 2020 · 3 comments

Comments

@markhats
Copy link

markhats commented Apr 16, 2020

I may be wrong, but it looks like when you call expression.evaluate({ x: 1 }), it compiles it into a JS function every time before evaluating. Is it possible to compile the expression just once and then call evaluate many times on the compiled function, passing in different values for x? This would make it much faster than compiling every time.

The equivalent math.js workflow would be:

const node = math.parse(expr)         // parse expression into a node tree
const code = node.compile()           // compile the node tree
const result = code.evaluate([scope]) // evaluate the code with an optional scope

where the final evaluate could be called multiple times without recompiling.

@markhats
Copy link
Author

I have got this working now I think. It's a little strange but the equivalent of the above math.js workflow is:

const expression = MathExpression.fromText(expr)
const evalFunc = expression.f()
const result = evalFunc([scope])

@dqnykamp
Copy link
Member

dqnykamp commented Apr 16, 2020 via email

@markhats
Copy link
Author

I guess I was just expecting something similar to math.js, with separate compile and evaluate functions. It's fine though, now I know how to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants