Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Function constructor (2)
(version: 1)
Constructing a Function() by passing args into the constructor or generated method
Comparing performance of:
Pass arg via constructor vs Pass args to function.
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
ab = new ArrayBuffer(1000*4); u32 = new Uint32Array(ab); code1 = `return function foo() { for (let i = 0; i < localCount; i++) { localArr[localCount]++; } }`; fnConstructor = new Function("localArr", "localCount", code1)(u32, 100); code2 = `return function foo(argArr, argCount) { for (let i = 0; i < argCount; i++) { argArr[argCount]++; } }`; fnWithArgs = new Function(code2)();
Tests:
Pass arg via constructor
fnConstructor();
Pass args to function.
fnWithArgs(u32, 100);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Pass arg via constructor
Pass args to function.
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Pass arg via constructor
37803588.0 Ops/sec
Pass args to function.
38003728.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON benchmark definition and explain what's being tested, compared options, pros/cons, library usage, special JavaScript features, and alternatives. **Benchmark Definition** The benchmark tests two ways to create a Function() instance in JavaScript: 1. **Passing arguments via constructor**: This approach involves creating a new ArrayBuffer, a Uint32Array from it, and then using the `new Function()` method with the array as an argument. 2. **Passing arguments directly in the function**: This approach creates another Function() instance without passing any arguments to its constructor. **Options Comparison** The two options differ in how they handle function creation: 1. **Passing arguments via constructor**: * Pros: Can be useful when working with large datasets or when you need more control over the function's environment. * Cons: May lead to performance issues due to the overhead of creating a new ArrayBuffer and Uint32Array, as well as potential issues with garbage collection. 2. **Passing arguments directly in the function**: * Pros: Can be faster and more memory-efficient than option 1, as it avoids creating unnecessary objects. * Cons: May not provide the same level of control over the function's environment as option 1. **Library Usage** There is no explicit library usage mentioned in the benchmark definition. However, the `Uint32Array` class is a part of the JavaScript standard library. **Special JavaScript Features** This benchmark uses two special features: 1. **Function constructor**: The `new Function()` method creates a new function object with the given string as its source code. 2. **Template literals**: The benchmark uses template literals (the backtick syntax) to create string literals with expressions inside them. The use of template literals is useful for creating strings with dynamic values, but it may lead to performance issues if not used carefully. **Alternatives** If you're interested in exploring alternative approaches or optimizations, here are a few options: 1. **Use `Function.prototype.call()` instead of `new Function()```**: This can eliminate the need for a separate function object and reduce overhead. 2. **Use a library like `es6-function-constructor`**: This library provides a more efficient way to create functions with constructors. 3. **Optimize using Just-In-Time (JIT) compilation**: Some JavaScript engines, like V8, use JIT compilation to optimize performance-critical code paths. Keep in mind that these alternatives may have trade-offs and require careful consideration of your specific use case and requirements.
Related benchmarks:
curry examples
isArray vs instanceof vs Symbol.iterator
isArray vs instanceOf vs Symbol.iterator vs Object.prototype.toString.call.slice
Arguments to Array - by Jeeeyul
Comments
Confirm delete:
Do you really want to delete benchmark?