Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs evalFunction vs function vs anonymous function
(version: 0)
Comparing performance of:
eval vs eval function vs function vs eval anonymous functions
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = Function("return 2 * 3"); var evalFunc = eval('(function(){ return () => 2 * 3; })();'); var evalAnonymousFunc = eval('() => 2 * 3');
Tests:
eval
eval("2 * 3");
eval function
evalFunc();
function
func()
eval anonymous functions
evalAnonymousFunc()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
eval
eval function
function
eval anonymous functions
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
eval
3263335.2 Ops/sec
eval function
45418352.0 Ops/sec
function
42455244.0 Ops/sec
eval anonymous functions
40408628.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of four different ways to write and execute a simple arithmetic expression: `2 * 3`. The test cases are: 1. `eval("2 * 3");` 2. `evalFunc();` (using a named function) 3. `func();` (using an anonymous named function, i.e., a regular function without a name) 4. `evalAnonymousFunc();` (using an anonymous expression) **Options Compared** The benchmark is comparing the performance of: * `eval()`: a built-in JavaScript function that evaluates a string as JavaScript code. * A named function: creating a new function using `Function("return 2 * 3")`. * An anonymous named function: creating a new function without a name using an arrow function expression, e.g., `(function(){ return () => 2 * 3; })()`. * An anonymous expression: using the syntax `() => 2 * 3`. **Pros and Cons** Here are some pros and cons of each approach: * `eval()`: Pros: + Easy to write + Works everywhere Cons: + Security risks (can evaluate arbitrary code) + Performance is often slow due to the need to parse the input string as JavaScript code. * Named function: Pros: + Faster than `eval()` for simple expressions + Less security risk than `eval()` Cons: + Requires more boilerplate code + Not as widely supported as other approaches * Anonymous named function (regular function): Pros: + Fast and efficient + Works everywhere Cons: + Less readable than other approaches + May not be clear that it's a regular function without the `()` syntax. * Anonymous expression: Pros: + Very concise and readable + Works in most modern browsers Cons: + Not as widely supported as other approaches + Can be slower due to the need for JavaScript engines to parse the input syntax. **Libraries** There are no libraries mentioned in this benchmark. However, it's worth noting that `eval()` can pose security risks if you're executing user-inputted code, which is why some browsers and frameworks have restrictions on its use. **Special JS Features or Syntax** This benchmark uses some special JavaScript features: * Arrow functions: used to create the anonymous named function. * The `Function` constructor: used to create a new function. * The `eval()` function with string input: used to evaluate a simple arithmetic expression. Overall, this benchmark is designed to test the performance of different ways to write and execute simple arithmetic expressions in JavaScript. It's likely intended to help developers choose the most efficient approach for their specific use case.
Related benchmarks:
window.eval function vs new Function
window.eval function vs new Function1
window.eval function vs new Function2
eval vs evalFunction vs function
Comments
Confirm delete:
Do you really want to delete benchmark?