Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function 211
(version: 1)
Comparing performance of:
eval vs new Function
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var func = new Function("return 5==5 && 3>2 && (2>1 || 6==4) && 'SSS' == 'SSS'");
Tests:
eval
eval("5==5 && 3>2 && (2>1 || 6==4) && 'SSS' == 'SSS'");
new Function
func();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
eval
new Function
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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The provided benchmark compares two approaches to evaluate a simple arithmetic expression: `eval` and creating a new Function object using the `new Function` constructor. **Options compared:** 1. **eval**: This method uses the built-in `eval()` function in JavaScript, which parses and executes the code contained in the string as JavaScript code. 2. **new Function**: This method creates a new function object from a given string, allowing you to specify the parameters and body of the function. **Pros and Cons:** * **eval**: * Pros: * Easy to use: `eval()` takes a simple syntax for evaluating expressions. * Built-in function: No need to worry about importing libraries or implementing parsing logic. * Cons: * Security concerns: `eval()` can execute arbitrary code, making it a potential security risk if used with untrusted input. * Performance overhead: `eval()` can be slower than other methods due to its dynamic nature and potential for additional checks. * **new Function**: * Pros: * More control: You have more control over the function creation process, allowing for better optimization or customization. * Security benefits: By creating a new function object, you can avoid using `eval()` and minimize security risks. * Cons: * Steeper learning curve: Creating a function object requires understanding of JavaScript's syntax and the Function constructor. * Potential overhead: Although generally faster than `eval()`, creating a function object still involves some parsing and execution steps. **Library usage:** There is no library used in this benchmark. The `new Function` approach relies solely on JavaScript's built-in functions. **Special JS feature or syntax:** None of the provided code uses any special JavaScript features or syntax. Both approaches are standard, widely supported constructs within the JavaScript language. Now that we've explored what's being tested and compared, let's discuss other alternatives: * **Function expression**: Instead of using `new Function`, you could use a function expression (e.g., `var func = (a, b) => a + b;`) to achieve similar results. This approach would be more concise but might not provide the same level of control as explicitly creating a new function object. * **Arrow functions**: Like function expressions, arrow functions (e.g., `const add = (a, b) => a + b;`) could also be used for simple arithmetic evaluations. However, they might have performance implications due to their syntax and potential optimizations by modern JavaScript engines. The benchmark results show that Firefox 106 on a Desktop with Ubuntu provides the best execution per second for both test cases (`new Function` and `eval`). The exact difference between the two approaches depends on factors like JavaScript engine optimizations, hardware capabilities, and specific use cases. If you're interested in exploring more benchmarks or understanding how to optimize your own JavaScript code, MeasureThat.net offers a wide range of microbenchmarks covering various aspects of JavaScript performance.
Related benchmarks:
eval vs new Function #2
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?