Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs mathjs (v11.4.0)
(version: 0)
Comparing performance of:
eval vs new Function vs mathjs
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.4.0/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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
8940238.0 Ops/sec
new Function
93345024.0 Ops/sec
mathjs
478123.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmarking test case. The test compares three different approaches to evaluate an expression: 1. **eval**: The built-in `eval` function in JavaScript, which executes a string as a program. 2. **new Function**: A new `Function` object created with the `new` keyword and a string literal that returns the desired value (in this case, "2 * 3"). 3. **mathjs** (version 11.4.0): The math library Math.js, which provides a way to evaluate mathematical expressions. **Options Compared** The three options are compared in terms of their performance (number of executions per second). The test is likely designed to determine which approach is the fastest for simple arithmetic expressions like "2 * 3". **Pros and Cons of Each Approach:** * **eval**: Pros: simple and widely supported. Cons: can be slow and insecure due to security vulnerabilities, especially when dealing with untrusted input. * **new Function**: Pros: provides a sandboxed execution environment, which can help prevent security issues. Cons: requires creating a new function object, which can be slower than using the built-in `eval`. * **mathjs**: Pros: optimized for mathematical computations and provides an additional layer of abstraction. Cons: requires including an external library, which may add overhead. **Library Description** The Math.js library (version 11.4.0) is a JavaScript library that provides a way to evaluate mathematical expressions. It allows users to define expressions as strings and execute them using a variety of methods, including `evaluate()` and other functions. **Special JS Features/Syntax** There are no specific special features or syntax mentioned in the benchmark definition or test cases. **Other Alternatives** If you're looking for alternatives to the built-in `eval` function, some options include: * Using a library like Math.js (as shown in the benchmark) for mathematical computations. * Utilizing a Just-In-Time (JIT) compiler like V8 or SpiderMonkey to optimize performance-critical code. * Implementing a recursive descent parser or other parsing technique to evaluate expressions. Keep in mind that each alternative has its own trade-offs and considerations, depending on your specific use case and requirements.
Related benchmarks:
eval vs new Function vs mathjs
eval vs new Function vs mathjs (evaluate) vs mathjs (chain)
eval vs new Function vs mathjs compiled
eval vs new Function vs mathjs 11
Comments
Confirm delete:
Do you really want to delete benchmark?