Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs mathjs
(version: 0)
Comparing performance of:
eval vs new Function vs mathjs
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.1/math.min.js"></script>
Script Preparation code:
var func = new Function("return 2 * 3");
Tests:
eval
eval("2 * 3");
new Function
func();
mathjs
math.evaluate("2 * 3");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
eval
new Function
mathjs
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
27518842.0 Ops/sec
new Function
241676720.0 Ops/sec
mathjs
1251867.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark for you. **Benchmark Overview** The test compares three different approaches to execute arithmetic expressions: 1. `eval` 2. Creating a new function using `Function` constructor 3. Using Math.js library's `evaluate` method **Options Comparison** Here are the pros and cons of each approach: 1. **`eval`** * Pros: Simple, widely supported, and fast execution. * Cons: + Security risks due to code injection vulnerabilities. + Performance overhead due to JavaScript engine's parsing and compilation steps. + Not suitable for production use cases where security is crucial. 2. **Creating a new function using `Function` constructor** * Pros: More secure than `eval`, as it doesn't execute arbitrary code. * Cons: + Performance overhead due to the creation of a new function object. + Less efficient than `eval` in terms of execution speed. 3. **Using Math.js library's `evaluate` method** * Pros: More secure and efficient than `eval`, as it uses a dedicated expression evaluation engine. * Cons: + Additional dependency on the Math.js library. + May have a higher performance overhead compared to native JavaScript implementations. **Library and Special JS Features** The test case using Math.js library's `evaluate` method includes the following: 1. **Math.js library**: A lightweight, fast, and secure mathematics library for JavaScript. It provides an expression evaluation engine that can be used to evaluate mathematical expressions without the need for a full-fledged JavaScript interpreter. 2. **No special JS features**: The test cases do not use any specialized JavaScript features like async/await, generators, or closures. **Other Alternatives** If you're looking for alternative approaches to execute arithmetic expressions, consider: 1. **Native JavaScript implementations**: Implementing arithmetic expressions using native JavaScript functions like `*` (multiplication), `/` (division), etc., which are generally faster and more efficient than the tested approaches. 2. **Expression evaluators libraries**: Other expression evaluation libraries like `esprima`, `recast`, or `astringify` can be used instead of Math.js library. Keep in mind that these alternatives might require more expertise and setup compared to using a dedicated expression evaluation library like Math.js.
Related benchmarks:
window.eval function vs new Function2
eval vs new Function vs mathjs (evaluate) vs mathjs (chain)
eval vs new Function vs mathjs compiled
eval vs new Function vs mathjs (v11.4.0)
Comments
Confirm delete:
Do you really want to delete benchmark?