Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Eval vs Function - GOSSTEST
(version: 0)
Comparing performance of:
eval(`(${theFunc})(3, 4)`) vs Function
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var theFunc = "function(a,b) { return a + b }"
Tests:
eval(`(${theFunc})(3, 4)`)
eval(`${theFunc}()`)
Function
new Function(`return ${theFunc}`)()(3,4)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval(`(${theFunc})(3, 4)`)
Function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
12 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0
Browser/OS:
Chrome 149 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval(`(${theFunc})(3, 4)`)
0.0 Ops/sec
Function
6140421.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches to evaluate a simple arithmetic expression: `eval` vs `Function`. The expression is defined as a JavaScript function `theFunc = "function(a,b) { return a + b }"`, which takes two arguments, `a` and `b`. **Options being compared** Two options are being compared: 1. **`eval`**: A built-in JavaScript function that evaluates a string as JavaScript code. 2. **`Function`**: A constructor that creates a new function object from a string. **Pros and Cons of each approach:** 1. **`eval`**: * Pros: Easy to use, can be faster since it's a native function. * Cons: Can pose security risks if the input string is not sanitized, as it can execute arbitrary code. Additionally, `eval` can lead to slower performance due to the overhead of parsing and executing the string as JavaScript code. 2. **`Function`**: * Pros: More secure than `eval`, as it creates a new function object that cannot be executed directly. Also, it's generally faster since it avoids the parsing overhead of `eval`. * Cons: Can be less convenient to use, as it requires creating a new function object and passing arguments manually. **Library usage** In this benchmark, neither of the options explicitly uses any external libraries. However, if we were to consider indirect dependencies, we could say that both approaches rely on the JavaScript engine's parsing and execution mechanisms, which are implemented by various browser engines (e.g., Blink in Chrome). **Special JS features or syntax** There are no special JavaScript features or syntax being tested in this benchmark. The focus is solely on the performance comparison between `eval` and `Function`. **Other alternatives** Other approaches to evaluate arithmetic expressions could include: * Using a dedicated math library like MathJax or KaTeX, which provide optimized implementations for mathematical expressions. * Implementing a custom parser or interpreter for the specific expression language being used (e.g., a simple recursive descent parser). * Utilizing specialized libraries like Google's Closure Compiler, which can optimize and compile JavaScript code for better performance. In summary, this benchmark provides a simple yet informative test case to compare the performance of `eval` and `Function` in evaluating a basic arithmetic expression.
Related benchmarks:
eval vs new Function
eval vs new Function proper
window.eval function vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
Comments
Confirm delete:
Do you really want to delete benchmark?