Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Function vs new Function with
(version: 0)
Comparing performance of:
new Function vs new Function with
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func = new Function("a", "b", "return a * b;"); var funcWith = new Function("$root", "with($root) { return a * b; }"); var context = { a: 2, b: 3 };
Tests:
new Function
func(context.a, context.b);
new Function with
funcWith(context);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Function
new Function with
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. **Benchmark Overview** The benchmark compares two approaches for creating functions in JavaScript: 1. `new Function("args")`: This approach creates a new function by parsing a string containing the function's code. The string must contain variable names (e.g., "a", "b") followed by an expression that returns the result of the variables multiplied together. 2. `new Function("with($root) { return a * b; }")`: This approach uses the `with` statement to create a new function that executes within a specific context. The `$root` variable is used as the global object (similar to `this` in traditional JavaScript). **Options Compared** The two approaches are compared in terms of performance, specifically: * Execution speed (`ExecutionsPerSecond`) * Browser and device platform support **Pros and Cons of Each Approach** 1. `new Function("args")`: * Pros: Simple, widely supported, and easy to implement. * Cons: Can be slower due to the overhead of parsing the function's code string. 2. `new Function("with($root) { ... }")`: * Pros: Can provide better performance due to optimized execution and reduced overhead. * Cons: Less intuitive and may require more complex setup, especially when working with external contexts. **Library or Framework** There is no specific library mentioned in the benchmark definition. However, it's worth noting that `with` statement can be used to create a new scope for executing code, which might be similar to how some libraries (e.g., Sandboxed JavaScript) work. **Special JS Feature or Syntax** The `with` statement is a special feature in JavaScript that allows execution within a specific context. It was introduced in ECMAScript 1999 and has been supported by most browsers since then. The `$root` variable is used as the global object, similar to `this` in traditional JavaScript. **Other Alternatives** For creating functions in JavaScript, other approaches include: 1. `Function constructor`: Similar to `new Function()`, but provides more control over function creation. 2. Arrow functions (`=>` syntax): A concise way to create small functions with a specific scope. 3. `bind()` method: Used to set the context of an existing function. In summary, the benchmark tests two approaches for creating functions in JavaScript and compares their performance. The `new Function("args")` approach is simpler but may be slower due to parsing overhead, while `new Function("with($root) { ... }")` provides better performance by optimized execution and reduced overhead.
Related benchmarks:
eval vs new Function v3
new Function vs Literal
window.eval function vs new Function1
new Function vs new Function with cache
Comments
Confirm delete:
Do you really want to delete benchmark?