HTML Preparation code:
AخA
 
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.1/math.min.js"></script>
Script Preparation code:
x
 
const supportedOperators = {
    '-': {
        precedence: 1,
        operate: (lhs, rhs) => lhs - rhs,
    },
    '+': {
        precedence: 1,
        operate: (lhs, rhs) => lhs + rhs,
    },
    '/': {
        precedence: 3,
        operate: (lhs, rhs) => lhs / rhs,
    },
    '*': {
        precedence: 4,
        operate: (lhs, rhs) => lhs * rhs,
    },
    '^': {
        precedence: 5,
        operate: (lhs, rhs) => lhs ** rhs,
    },
};
function isOperator(value) {
    return !!supportedOperators[value];
}
function isOperand(value) {
    return !Number.isNaN(+value);
}
function getPrecedence(operator) {
    const supportedOperator = supportedOperators[operator];
    if (!supportedOperator) {
        return -1;
    }
    return supportedOperator.precedence;
}
function evaluateLastOperation(operators, operands) {
    const operator = supportedOperators[operators.pop()];
    const rhs = operands.pop();
    const lhs = operands.pop();
    return operator.operate(+lhs, +rhs);
}
function tokenizeExpression(expr) {
    const currentTokens = [];
    let currentValue = '';
    for (let i = 0; i < expr.length; i += 1) {
        if (expr[i] === ' ') {
            if (currentValue) {
                currentTokens.push(currentValue);
                currentValue = '';
            }
        } else if (expr[i] === ')') {
            if (currentValue) {
                currentTokens.push(currentValue);
                currentValue = '';
            }
            currentTokens.push(')');
        } else if (isOperand(expr[i]) ||
            (expr[i] === '.' && currentValue.indexOf('.') === -1 && expr[i + 1]) ||
            (expr[i] === '-' && currentValue.indexOf('-') === -1 && isOperand(expr[i + 1]))) {
            currentValue += expr[i];
        } else if (expr[i] === '(' || expr[i] === ')' || isOperator(expr[i])) {
            currentTokens.push(expr[i]);
        }
    }
    if (currentValue) {
        currentTokens.push(currentValue);
    }
    return currentTokens;
}
function evaluateExpression(expr) {
    const tokens = tokenizeExpression(expr);
    const operands = [];
    const operators = [];
    tokens.forEach((token) => {
        if (isOperand(token)) {
            operands.push(token);
        } else if (token === '(') {
            operators.push(token);
        } else if (token === ')') {
            let operatorTop = operators[operators.length - 1];
            while (operators.length && operatorTop !== '(') {
                operands.push(evaluateLastOperation(operators, operands));
                operatorTop = operators[operators.length - 1];
            }
            operators.pop();
        } else if (isOperator(token)) {
            let operatorTop = operators[operators.length - 1];
            while (operators.length && operatorTop !== '(' && getPrecedence(token) <= getPrecedence(operatorTop)) {
                operands.push(evaluateLastOperation(operators, operands));
                operatorTop = operators[operators.length - 1];
            }
            operators.push(token);
        }
    });
    while (operators.length) {
        operands.push(evaluateLastOperation(operators, operands));
    }
    return operands[0];
}
var testMathExpression = `142 * ( 2 / 0.9 ) ^ -1.2 * 0.9938 ^ 50`;
var func = new Function(`return ${testMathExpression}`);
Tests:
  • eval

     
    eval(testMathExpression);
  • new Function

     
    func();
  • mathjs

     
    math.evaluate(testMathExpression);
  • evaluateExpression

     
    evaluateExpression(testMathExpression);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    eval
    new Function
    mathjs
    evaluateExpression

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (iPhone; CPU iPhone OS 17_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1
Mobile Safari 17 on iOS 17.2.1
View result in a separate tab
Test name Executions per second
eval 7952271.0 Ops/sec
new Function 29537648.0 Ops/sec
mathjs 154031.1 Ops/sec
evaluateExpression 170868.0 Ops/sec