Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function vs mathjs v2
(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/6.6.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):
I'll explain what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares three different approaches to evaluate a simple mathematical expression: `2 * 3`. The expressions are: 1. `eval("2 * 3")` 2. `new Function("return 2 * 3")` (using a self-invoking function) 3. `compiled.evaluate()` where `mathjs` is used to compile and evaluate the expression. **Options Compared** The benchmark compares the execution performance of these three approaches on a Chrome 104 browser on a desktop platform. **Pros and Cons of Each Approach** 1. **eval**: The `eval()` function parses the code as JavaScript and executes it immediately. It's a lightweight way to execute code, but it can pose security risks if not used carefully. * Pros: Fast, easy to use * Cons: Security concerns, potential performance issues with complex expressions 2. **new Function**: This approach uses a self-invoking function to evaluate the expression. It provides better security than `eval` since the code is executed in a sandboxed environment. * Pros: Safer, can be faster for simple expressions * Cons: Requires more boilerplate code, may not perform well with complex expressions 3. **mathjs**: Math.js is a library that compiles mathematical expressions into optimized machine code. It's designed to improve performance and security when working with math operations. * Pros: High performance, secure, easy to use * Cons: Requires including the math.js library, may not be suitable for all types of expressions **Library Use** The benchmark uses Math.js version 6.6.1. Math.js provides a way to compile and evaluate mathematical expressions in a safe and efficient manner. **Special JS Feature or Syntax** None mentioned in this benchmark. **Other Alternatives** Other alternatives for evaluating mathematical expressions include: * `Function()` with a callback function (similar to the `new Function` approach) * Using a JavaScript engine like V8 or SpiderMonkey * Utilizing specialized libraries like NumJS or Math.NET Keep in mind that each approach has its trade-offs, and the best choice depends 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?