Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Eval va function
(version: 0)
Comparing performance of:
Eval vs Function
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Eval
eval("2 * 3")
Function
new Function("return 2 * 3")()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Eval
Function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Eval
2689221.8 Ops/sec
Function
1210368.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript microbenchmark test on MeasureThat.net. **What is being tested?** The provided benchmark tests two different ways of evaluating simple arithmetic expressions in JavaScript: using the `eval()` function and creating a new function using the `Function` constructor. The benchmark aims to measure which approach is faster, more efficient, or has better performance characteristics. **Options compared:** 1. **`eval()`**: This method uses a built-in JavaScript function that parses and executes the provided string as JavaScript code. 2. **`new Function()`:** This method creates a new anonymous function from the provided string, which can then be executed using the `()` operator. **Pros and Cons of each approach:** 1. **`eval()`**: * Pros: + Simple to use: just pass the expression as a string. + Works with most JavaScript engines. * Cons: + Can be slower due to security features (e.g., sandboxing) that slow down `eval()`. + Can introduce vulnerabilities if the input is not sanitized or validated. 2. **`new Function()`**: * Pros: + More efficient, as it avoids security features that slow down `eval()`. + Safer than `eval()`, as the resulting function is sandboxed and cannot access external resources. * Cons: + Requires more boilerplate code to create a new function from an expression. **Library and purpose:** None of the benchmark tests use any specific libraries. The focus is solely on comparing the performance of these two JavaScript built-in functions. **Special JS features or syntax:** There are no special JavaScript features or syntaxes used in this benchmark. It's a straightforward comparison of two basic evaluation methods. **Other alternatives:** For similar purposes, you might consider using other approaches: 1. **`eval()` with a safer alternative:** Some libraries (e.g., `safe-eval`) provide a safer version of `eval()` that can help mitigate security concerns. 2. **`new Function()` with optimization:** You could experiment with optimizing the `Function` constructor, such as by using `String.prototype.replace()` to remove unnecessary whitespace or characters from the input string. **Benchmark preparation code:** The provided benchmark preparation code is empty (`"Script Preparation Code": null, "Html Preparation Code": null`). This means that no additional setup or configuration is required before running the benchmark. In summary, the MeasureThat.net JavaScript microbenchmark tests the performance of `eval()` and `new Function()`, two fundamental evaluation methods in JavaScript. By comparing these approaches, users can gain insights into the efficiency and security characteristics of each method.
Related benchmarks:
eval vs new Function #2
window.eval function vs new Function2
eval vs evalFunction vs function
eval vs evalFunction vs function vs anonymous function
Comments
Confirm delete:
Do you really want to delete benchmark?