Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function() vs mathjs (evaluate) vs mathjs (chain)
(version: 0)
Comparing performance of:
eval vs new Function vs mathjs vs mathjs-chain vs native power
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/mathjs@13.0.3/lib/browser/math.js"></script>
Script Preparation code:
var func = new Function("return 2 ** 3");
Tests:
eval
let x = eval("2 ** 3");
new Function
let x = func();
mathjs
let x = math.evaluate("2 ** 3");
mathjs-chain
let x = math.chain(2).pow(3)
native power
let x = 2 ** 3
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
eval
new Function
mathjs
mathjs-chain
native power
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0
Browser/OS:
Firefox 52 on Windows XP
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
308220.8 Ops/sec
new Function
216814864.0 Ops/sec
mathjs
0.0 Ops/sec
mathjs-chain
0.0 Ops/sec
native power
849834944.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark is designed to compare the performance of different approaches for executing simple mathematical expressions in JavaScript. **Script Preparation Code and Html Preparation Code** The script preparation code defines a new function `func` using the `Function` constructor, which returns the value `2 ** 3`. The html preparation code includes a reference to the Math.js library, which is used later in the benchmark. **Individual Test Cases** The benchmark consists of six test cases, each representing a different approach for executing a simple mathematical expression: 1. **eval**: Uses the built-in `eval` function to execute the expression `"2 ** 3"`. 2. **new Function**: Creates a new function using the `Function` constructor and calls it with the expression `"2 ** 3"` as an argument. 3. **mathjs (evaluate)**: Uses the Math.js library's `evaluate` method to execute the expression `"2 ** 3"`. 4. **mathjs (chain)**: Uses the Math.js library's `chain` method to create a pipeline of operations, starting with `pow(3)` and then exponentiating 2. 5. **native power**: Executes the expression directly using the exponentiation operator (`**`). 6. **empty function call**: This test case is skipped, as its execution time is reported as 0. **Library: Math.js** Math.js is a lightweight JavaScript library for mathematical computations. It provides a simple and intuitive API for performing various mathematical operations, including exponentiation. In this benchmark, Math.js is used to execute expressions using the `evaluate` and `chain` methods. **Special JS Feature/Syntax: None** None of the test cases use special JavaScript features or syntax that would affect their execution performance. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: * **eval**: Pros: simple to implement, well-supported in most browsers. Cons: security risks due to arbitrary code execution, performance overhead. * **new Function**: Pros: provides more control over the execution environment, can be used for more complex computations. Cons: slower than native code execution, may incur additional overhead. * **mathjs (evaluate)**: Pros: provides a lightweight and efficient way to execute mathematical expressions, supports various input formats. Cons: requires additional library dependencies, may have performance overhead due to interpretation. * **mathjs (chain)**: Pros: provides a flexible pipeline-based approach for complex computations, can be optimized for performance. Cons: may require more memory allocation and deallocation, adds library dependency. **Other Alternatives** Some alternative approaches that could be used in similar benchmarks include: * Using the `Number` function with binary exponentiation algorithms (e.g., Montgomery ladder) * Implementing a simple recursive descent parser or lexically-scanned compiler for arithmetic expressions * Using a Just-In-Time (JIT) compiler to optimize the execution of mathematical expressions However, these alternatives would likely require significant modifications to the benchmark and may not provide comparable results.
Related benchmarks:
eval vs new Function vs mathjs
window.eval function vs new Function
eval vs new Function vs mathjs (evaluate) vs mathjs (chain)
eval vs new Function vs mathjs compiled
Comments
Confirm delete:
Do you really want to delete benchmark?