Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function vs Eval vs Getter
(version: 0)
Compares the speed of functions, evals and Getters.
Comparing performance of:
Function vs Eval vs Getter
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var fun = function(i) { return i; }; var evalCode = 'i;'; Object.defineProperty(window, 'getterBenchmark', { get value() { return 'hi'; } });
Tests:
Function
for(let i = 0; i < 5; i++) { fun(i); };
Eval
for(let i = 0; i < 5; i++) { eval(evalCode); };
Getter
for(let i = 0; i < 5; i++) { getterBenchmark; };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Function
Eval
Getter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Function
8632218.0 Ops/sec
Eval
1245728.4 Ops/sec
Getter
4701928.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark. **Benchmark Overview** The benchmark compares the speed of three approaches: functions, `eval` expressions, and getters (also known as accessed properties). The test is designed to measure the execution time of each approach on a specific script and HTML setup. **Script Preparation Code** The provided script preparation code defines: 1. A function named `fun` that takes an argument `i` and returns its value. 2. An `eval` expression that evaluates the string `'i;'`, effectively returning the value of the variable `i`. 3. A getter property named `getterBenchmark` on the global object (`window`). The getter function returns the string `'hi'`. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** Each test case consists of a benchmark definition and a test name: 1. **Function**: This test case defines a loop that calls the `fun` function five times, passing the argument `i`. 2. **Eval**: This test case defines a loop that evaluates the `evalCode` expression three times, effectively calling the `fun` function three times. 3. **Getter**: This test case simply calls the getter property `getterBenchmark` five times. **Approach Comparison** The benchmark compares the speed of each approach: * Functions: The `fun` function is defined explicitly, which can lead to better optimization and caching. * `eval`: `eval` expressions can be slow because they involve parsing and executing arbitrary code at runtime. However, modern engines often optimize `eval` usage. * Getters: Accessing properties through getters can be faster than calling functions or using `eval`, as the browser can cache and reuse the getter function. **Pros and Cons** * Functions: + Pros: Better optimization, caching, and performance. + Cons: May require more memory to store the function. * `eval`: + Pros: Can be useful for dynamic code evaluation or executing user-supplied code. + Cons: Generally slower than functions and getters due to parsing and execution overhead. * Getters: + Pros: Faster than functions and `eval`, with potential caching benefits. + Cons: May require more memory to store the getter property. **Library** None of the test cases use any external libraries. The script preparation code defines the three approaches independently, while the HTML preparation code is not provided. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you were to rewrite this benchmark, you might consider using alternative approaches: * Use a loop instead of `for` loops for more control over iteration. * Add error handling and boundary checking to test edge cases. * Compare additional functions or algorithms to gain further insights into performance differences. * Measure the impact of caching, memoization, or other optimization techniques on each approach. Feel free to ask if you have any questions or need further clarification!
Related benchmarks:
eval vs new Function v3
window.eval vs new Function
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?