Expression evaluator performance comparison (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sval@0.6.3/dist/sval.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/expr-eval@2.0.2/dist/bundle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjs@12.4.1/lib/browser/math.js"></script>

Setup

// Create shared context
const context = { 
	c_6853e66dffb3392513d4f9bb: '19998269322',
    c_6853e66dffb3392513d4f9bc: 'KERBR00043981',
    c_6853e66dffb3392513d4f9bd: '584',
    c_6853e66dffb3392513d4f9be: '01/02/2024 T5:23:51 AM GMT',
    c_6853e66dffb3392513d4f9bf: 'Sprinklr',
    c_6853e66dffb3392513d4f9c0: 'Brazil',
    c_6853e66dffb3392513d4f9c1: '5519998269322', };
const expression = 'c_6853e66dffb3392513d4f9bd > 600 ? "High" : "Low"';
const evalExpression = '584 > 600 ? "High" : "Low"';


// Function to evaluate with Sval without AST caching
const interpreter = new Sval({
  ecmaVer: 11,
  sourceType: "script",
  sandBox: true,
});

interpreter.import(context);

const runSvalWithoutASTCaching = (expr) => {
  interpreter.run(`exports.__output__ = ${expr}`);
  return interpreter.exports.__output__;
}

// Function to evaluate with Sval with AST caching
const ASTMapCache = new Map();
const interpreterwithCaching = new Sval({
  ecmaVer: 11,
  sourceType: "script",
  sandBox: true,
});

interpreterwithCaching.import(context);

const runSvalWithASTCaching = (expression) => {
	const svalScript = `exports.__output__ = ${expression}`;

if (!ASTMapCache.has(svalScript)) {
    ASTMapCache.set(svalScript, interpreterwithCaching.parse(svalScript));
}

interpreterwithCaching.run(ASTMapCache.get(svalScript));
}

// Expr-Eval
const exprEvalParser = new window.exprEval.Parser();
const exprEvalAst = exprEvalParser.parse(expression);
const runExprEval = () => {
  return exprEvalAst.evaluate(context);
}

// Using Math.js
const runMathJS = (expression) => {
	const mathjsCompiled = math.compile(expression);
  return mathjsCompiled.evaluate(context);
}

Test runner

Ready to run.

Testing in
TestOps/sec
eval
eval(evalExpression);
ready
Sval without AST caching
runSvalWithoutASTCaching(expression);
ready
Sval with AST caching
runSvalWithASTCaching(expression);
ready
Math.js
runMathJS(expression);
ready
Expr-eval
runExprEval(expression);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.