Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
eval vs new Function v5
(version: 0)
Comparing performance of:
new Function vs func
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var funcInt = Function("x", "return x % 33 == 0"); var funcNat = function(x) { return x % 33 == 0; };
Tests:
new Function
var sum = 0; for (var i = 0; i < 1000; ++i) { sum += funcInt(i); }
func
var sum = 0; for (var i = 0; i < 1000; ++i) { sum += funcNat(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Function
func
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 what's being tested in this JavaScript benchmark. **Benchmark Definition** The benchmark defines two different ways to create and use a function: `new Function` and a custom-defined function named `func`. The script preparation code provides two functions, `funcInt` and `funcNat`, which are used as examples in the test cases. `funcInt` is a built-in JavaScript function that returns the remainder of `x` divided by 33 when called with an integer argument. `funcNat` is a custom-defined function that achieves the same result. **Options Compared** The benchmark compares two approaches: 1. **new Function**: Creates a new function object using the `Function` constructor, which takes a string of code as its first argument and a variable name as its second argument. 2. **Custom-defined function (func)**: Defines a new function named `func` with a single parameter `x`, and then uses this function in the test cases. **Pros and Cons** * **new Function**: Pros: + Can be used to dynamically create functions at runtime. + Can capture variables from the surrounding scope. Cons: + Creates a new function object, which can lead to higher memory usage. + May incur additional overhead due to the constructor call. * **Custom-defined function (func)**: Pros: + Avoids the overhead of creating a new function object. + Can be more efficient in terms of memory usage. Cons: + Requires defining the function explicitly, which can add complexity. + May not capture variables from the surrounding scope. **Other Considerations** * The benchmark uses a simple arithmetic operation (modulus) to test the performance of each approach. In reality, more complex functions and operations may have different performance characteristics. * The benchmark does not consider other factors that might affect performance, such as function length, number of parameters, or caching behavior. **Library/Function Details** None mentioned in this specific benchmark. However, it's worth noting that the `Function` constructor is a built-in JavaScript feature, and its usage can be complex due to security concerns (e.g., eval). No special JS features or syntax are used in this benchmark.
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?