Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs mathjs 11
(version: 0)
Comparing performance of:
eval vs new Function vs mathjs
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.0.1/math.min.js"></script>
Script Preparation code:
var func = new Function("return 2 * 3"); var compiled=math.compile("2*3")
Tests:
eval
eval("2 * 3");
new Function
func();
mathjs
compiled.evaluate();
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares three different ways to execute a simple arithmetic expression: `eval()`, `new Function()`, and `mathjs`. The benchmark aims to measure which approach is the fastest, with the highest number of executions per second. **Options Compared** 1. **`eval()`**: Evaluates a string as JavaScript code. 2. **`new Function()`**: Creates a new function from a given string, which can be executed like any other function. 3. **`mathjs`**: Uses the MathJS library to compile and execute mathematical expressions. **Pros and Cons of Each Approach** 1. **`eval()`**: * Pros: Simple and easy to use, no need to create a new function object. * Cons: Can be slower due to security checks and compilation overhead. 2. **`new Function()`**: * Pros: Faster than `eval()`, as it avoids unnecessary security checks. * Cons: Requires creating a new function object, which can lead to performance overhead. 3. **`mathjs`**: * Pros: Optimized for mathematical expressions, can take advantage of Just-In-Time (JIT) compilation. * Cons: Adds an extra library dependency and may require additional setup. **Library Used: MathJS** MathJS is a JavaScript library specifically designed for high-performance mathematical computations. It provides an optimized way to evaluate mathematical expressions, taking into account various optimization techniques like JIT compilation. The library is used in this benchmark to compare its performance with `eval()` and `new Function()`. **Special JS Feature/Syntax Used: None** There are no special JavaScript features or syntaxes used in this benchmark. **Benchmark Preparation Code Explanation** The script preparation code creates two variables: 1. `func`: A new function created using the `new Function()` constructor, which takes a single expression (`"return 2 * 3"`). This function is then executed immediately. 2. `compiled`: The result of compiling a mathematical expression using MathJS (`"2*3"`). The HTML preparation code includes the necessary MathJS library file to enable its functionality. **Benchmark Results** The latest benchmark results show that: 1. **`new Function()`**: Outperforms both `eval()` and `mathjs`, with the highest number of executions per second. 2. **`mathjs`**: Performs better than `eval()`, but is slower than `new Function()`. 3. **`eval()`**: The slowest option, likely due to security checks and compilation overhead. **Other Alternatives** If you're looking for alternative approaches, consider the following: 1. **`Function()`**: Similar to `new Function()`, but without the need for a new function object. 2. **`Babel()`**: A transpiler that can optimize JavaScript code for better performance. 3. **`ES6 template literals`**: Can be used to evaluate mathematical expressions in a more concise way, but may not offer significant performance improvements over `new Function()`. Keep in mind that the choice of approach depends on your specific use case and performance requirements.
Related benchmarks:
eval vs new Function vs mathjs (evaluate) vs mathjs (chain)
eval vs new Function vs mathjs compiled
eval vs new Function vs mathjs v2
eval vs new Function vs mathjs (v11.4.0)
Comments
Confirm delete:
Do you really want to delete benchmark?