Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Function() vs native
(version: 0)
Comparing performance of:
new Function vs native fn
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var func1 = new Function("d", "return Math.log(2, d)"); var func2 = (d) => Math.log(2, d);
Tests:
new Function
let x = func1(200);
native fn
let x = func2(200)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new Function
native fn
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Function
16058712.0 Ops/sec
native fn
6628096.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Overview** The benchmark compares the performance of two approaches: creating a new function using the `new Function()` constructor and defining a native function using an arrow function syntax (`(d) => ...`). The test cases measure how many executions per second each approach can perform on a simple mathematical operation involving logarithms. **What's being tested** The benchmark tests the performance of two different ways to create a function: 1. **`new Function()`**: This approach creates a new function using the `Function` constructor. The syntax is: `var func = new Function('param1', 'return expression')`. 2. **Native function definition**: This approach defines a native function using an arrow function syntax (`(param) => expression`). In this case, the test uses the simple `(d) => Math.log(2, d)` syntax. **Options compared** The benchmark compares two options: * `new Function()` (Option 1): Creates a new function dynamically at runtime. * Native function definition using arrow functions (Option 2): Defines a native function that can be executed directly without the need for dynamic creation. **Pros and Cons of each approach:** 1. **`new Function()`** * Pros: + Allows for more flexibility in defining custom functions with varying parameters. + Can be useful when working with legacy code or dynamic data. * Cons: + Performance overhead due to dynamic function creation at runtime. + May incur additional memory allocations and garbage collection. 2. **Native function definition using arrow functions** * Pros: + Faster execution performance since native functions are compiled by the JavaScript engine ahead of time. + Memory-efficient, as there's no additional allocation for a new function object. * Cons: + Less flexible than `new Function()`, as you're limited to defining functions with specific parameter names. **Library and special JS feature** There is no library or special JS feature being used in this benchmark. It only relies on standard JavaScript features, such as the `Function` constructor and arrow function syntax. **Alternative approaches** If you want to explore alternative approaches for creating functions in JavaScript: 1. **Closure**: You can define a closure by returning an expression from a higher-scoped function that captures variables from its surrounding scope. 2. **Constructor functions**: Instead of using the `new Function()` constructor, you can use a constructor function (e.g., `class Func { ... }`) to create new functions dynamically. Keep in mind that these alternatives might have different performance characteristics compared to native function definition using arrow functions.
Related benchmarks:
Classes vs functions
Math.pow() vs exponentiation operator
Math.pow vs Exponentiation vs Multiplication
Math.pow vs Exponentiation vs Multiplication pow 4
Exponentiation vs Math.pow test
Comments
Confirm delete:
Do you really want to delete benchmark?