Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function fork
(version: 0)
Comparing performance of:
eval vs new Function
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("return true && true && (false || true)");
Tests:
eval
eval("true && true && (false || true)");
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 break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Definition:** The benchmark definition is a JSON object that represents the test case. It includes: * `Name`: A unique name for the benchmark. * `Description`: An optional description of the benchmark (not used in this example). * `Script Preparation Code`: The JavaScript code that sets up the environment for the benchmark. In this case, it creates a new function using `new Function` and assigns it to the variable `func`. * `Html Preparation Code`: An optional HTML code that might be used to prepare the testing environment (not used in this example). **Individual Test Cases:** The benchmark includes two test cases: 1. **eval** * `Benchmark Definition`: The JavaScript code being tested, which uses the built-in `eval` function. * `Test Name`: A unique name for the test case. 2. **new Function** * `Benchmark Definition`: The same JavaScript code as in the first test case, but this time using the `new Function` constructor instead of `eval`. * `Test Name`: A unique name for the test case. **Comparison:** The benchmark is comparing the performance of two approaches: 1. **eval**: Using the built-in `eval` function to execute a JavaScript code string. 2. **new Function**: Creating a new function using the `new Function` constructor and executing it. **Pros and Cons:** * **eval**: + Pros: - Easy to use, as it's a built-in function that requires minimal code. + Cons: - Can be slower due to security checks and parsing overhead. - Less safe, as it can execute arbitrary code. * **new Function**: + Pros: - More controlled and safer than `eval`, as it creates a new function with specific scope and visibility. + Cons: - Requires more code and setup compared to `eval`. - Might be slower due to the overhead of creating a new function. **Other Considerations:** * **Context**: The benchmark is likely being used to compare the performance of different JavaScript engines or interpreters, as indicated by the presence of browser-specific information in the result. * **Safety**: As mentioned earlier, `eval` can be less safe than `new Function`, which might be a consideration depending on the use case. **Libraries and Special Features:** There is no library being used in this benchmark. However, it's worth noting that `new Function` uses a similar syntax to function expressions in JavaScript (e.g., `function foo(x) { ... }`), but with additional overhead due to creating a new function object. **Alternatives:** If you wanted to create a similar benchmark, you might consider using: * **Function expressions**: Instead of `new Function`, you could use function expressions like `function foo() { ... }` and compare their performance. * **Arrow functions**: If you're targeting modern JavaScript environments (ES6+), arrow functions (`() => { ... }`) might provide a similar balance between control and simplicity. * **Template literals**: Another option would be to use template literals (`${expression}`) instead of `eval`, which can provide similar functionality with fewer safety concerns. Keep in mind that the choice of alternative will depend on your specific requirements, target JavaScript version, and desired performance characteristics.
Related benchmarks:
eval vs new Function
eval vs new Function v3
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?