function predefined () { return this; }
var something = 'something';
arrowFn = () => something
bindFn = predefined.bind(something)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Arrow function | |
Bind function |
Test name | Executions per second |
---|---|
Arrow function | 4371583.5 Ops/sec |
Bind function | 4247123.5 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark compares the performance of two approaches to create a function in JavaScript:
arrowFn = () => something
) to create a function.bind()
method to create a new function from an existing one (bindFn = predefined.bind(something)
).Options Compared
Two options are being compared:
() => something
)predefined.bind(something)
) + Function Constructor, which is used to create a new function by passing an existing function as an argument.Pros and Cons of Each Approach:
this
context resolutionthis
binding)this
contextbind()
method, which can lead to memory allocation and garbage collection overhead.Other Considerations
new Function('return something;')
) can also create a new function object and may have similar performance characteristics to bind function creation.Special JavaScript Features or Syntax
None of the test cases use special JavaScript features or syntax that would affect their execution. The focus is on comparing the performance of two fundamental approaches to create functions in JavaScript.
Other Alternatives
If you want to compare the performance of other ways to create functions, such as:
(function() { return something; }())
var fn = function(something) { return something; };
(function(something) { return function() { return something; }; })(something)
(async function() { return something; })()
You can add these alternatives to the benchmark definition and test cases.
I hope this explanation helps!