Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Function vs new Function with cache
(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 context = { a: 2, b: 3 }; var func = new Function("a", "b", "return a * b;"); var funcWith = new Function("$root", "with($root) { return function () { return a * b; } }")(context);
Tests:
new Function
func(context.a, context.b);
new Function with
funcWith();
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 its components. **Benchmark Definition** The benchmark is designed to compare two approaches for creating JavaScript functions: 1. `new Function("a", "b", "return a * b;")` 2. `new Function("$root", "with($root) { return function () { return a * b; } }(context)")` These two approaches are being compared in terms of performance. **Options Compared** The two options are: 1. **`new Function("a", "b", "return a * b;")`**: This approach creates a new function by stringifying the function body and passing it to `Function`. The function's variables (`a` and `b`) are passed as arguments. 2. **`new Function("$root", "with($root) { return function () { return a * b; } }(context)")`**: This approach uses the `with` statement with an object context, and then creates a new function using `Function`. The `$root` parameter is used to access the object's properties. **Pros and Cons** 1. **`new Function("a", "b", "return a * b;")`**: * Pros: Simple and straightforward, no additional dependencies required. * Cons: May be slower due to stringification overhead, and limited flexibility in variable naming. 2. **`new Function("$root", "with($root) { return function () { return a * b; } }(context)")`**: * Pros: More flexible, as it allows using the `with` statement with an object context, which can be useful for complex logic. * Cons: Requires additional dependencies (the `$root` variable), and may introduce performance overhead due to the `with` statement. **Library and Purpose** The only library used in this benchmark is the JavaScript `Function` constructor. The purpose of `Function` is to create a new function with optional arguments and body. **Special JS Feature or Syntax** The benchmark uses the `with` statement, which allows accessing an object's properties using the name of the property as a prefix. This feature is specific to ECMAScript 5 (ES5) and later versions. **Other Alternatives** If you're looking for alternatives to these two approaches: * For simple function creation without stringification overhead, consider using a library like `bindgen` or creating an anonymous function with `function() { ... }`. * For more complex logic and flexibility in variable naming, consider using a JavaScript macro system like `esmacros` or a dynamic import-based approach. **Benchmark Preparation Code** The script preparation code creates two variables: `context` with properties `a` and `b`, set to 2 and 3, respectively. Two functions are then created: `func` and `funcWith`. The first function is created using the `new Function` constructor with explicit arguments, while the second function uses the `$root` variable and the `with` statement. **Individual Test Cases** The benchmark consists of two test cases: 1. Running the `func` function, passing `context.a` and `context.b` as arguments. 2. Running the `funcWith` function, without any additional arguments. These test cases are executed multiple times to measure performance.
Related benchmarks:
eval vs new Function v3
new Function vs Literal
new Function vs new Function with
JavaScript nested namespace vs simple namespace
Comments
Confirm delete:
Do you really want to delete benchmark?