Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs mathjs compiled
(version: 0)
Comparing performance of:
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/6.6.1/math.min.js"></script>
Script Preparation code:
var func = new Function("return 2 * 3"); var code = math.compile("2 * 3");
Tests:
new Function
func();
mathjs
code.evaluate();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
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
new Function
213487904.0 Ops/sec
mathjs
111066840.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and other considerations. **Benchmark Overview** The benchmark compares three different approaches to execute a simple mathematical expression: 1. `new Function`: Creating a new function using the `Function` constructor. 2. `mathjs.compile()`: Compiling a mathematical expression using Math.js library. 3. `eval()` : Evaluating a string using the built-in `eval()` function. **Options Compared** The two options being compared in this benchmark are: 1. `new Function` and `eval()`, which are both used to execute JavaScript code directly. 2. `mathjs.compile()` and `eval()`, where Math.js is used to compile a mathematical expression, and then executed using the `evaluate()` method. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **new Function** * Pros: Creates an anonymous function that can be used like any other function. * Cons: Can be slower due to the overhead of creating a new function object. 2. **eval()** * Pros: Fast and lightweight, but can pose security risks if used with untrusted input. * Cons: Can be slow for complex expressions or large inputs. 3. **mathjs.compile()** * Pros: Compiles mathematical expressions to machine code, making it faster than `new Function` or `eval()`. * Cons: Requires an external library (Math.js), and the compilation process can add overhead. **Library: Math.js** Math.js is a JavaScript library that provides a way to compile and execute mathematical expressions. It's designed to be fast and efficient, making it suitable for performance-critical applications. In this benchmark, Math.js is used to compile a simple mathematical expression (`"2 * 3"`), which is then executed using the `evaluate()` method. **Special JS Feature: None** There are no special JavaScript features or syntaxes being tested in this benchmark. The expressions being evaluated are straightforward and don't rely on any advanced JavaScript features. **Other Alternatives** If you're looking for alternatives to Math.js, here are a few options: 1. **Babel**: A transpiler that can compile JavaScript code to machine code. 2. **ES6 Transpilers**: Some ES6 transpilers, like Babel or TypeScript, can also compile JavaScript code to machine code. 3. **Other libraries**: There are other libraries available that provide similar functionality to Math.js, such as `numjs` or `js-expr`. Keep in mind that each of these alternatives has its own strengths and weaknesses, and may not offer the same level of performance or ease of use as Math.js. **Benchmark Preparation Code** The benchmark preparation code is quite simple: ```javascript var func = new Function("return 2 * 3"); var code = math.compile("2 * 3"); ``` This code creates a new function using `new Function`, compiles the mathematical expression `"2 * 3"` using Math.js, and assigns the resulting function or compiled code to variables. **Individual Test Cases** The benchmark consists of two test cases: 1. **"new Function"`**: This test case executes the `func()` call, which uses the `new Function` constructor. 2. **"mathjs"`**: This test case executes the `code.evaluate()` call, which uses Math.js to compile the mathematical expression and then execute it using the `evaluate()` method. These test cases are designed to measure the performance of each approach in executing a simple mathematical expression.
Related benchmarks:
eval vs new Function vs mathjs
eval vs new Function vs mathjs (evaluate) vs mathjs (chain)
eval vs new Function vs mathjs 11
eval vs new Function vs mathjs v2
Comments
Confirm delete:
Do you really want to delete benchmark?