var fn1 = (a,b) => a + b
var fn2 = function(a,b){ return a + b }
fn1(1,1)
fn2(1,1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
arrow fn | |
function |
Test name | Executions per second |
---|---|
arrow fn | 32860244.0 Ops/sec |
function | 33178506.0 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Benchmark Definition
The benchmark definition defines two functions, fn1
and fn2
, which are compared in terms of performance. The difference between them lies in how they're defined:
fn1
: This is an arrow function (also known as a lambda function or shorthand function), defined using the syntax (a,b) => a + b
. Arrow functions were introduced in ECMAScript 2015 (ES6) and provide a concise way to define small, one-time-use functions.fn2
: This is a traditional function definition, defined using the function
keyword followed by parameters and a body: function(a,b){ return a + b }
. Traditional function definitions have been around since the early days of JavaScript.Options Compared
The benchmark compares the performance of:
fn1
): This option uses an arrow function, which is expected to be more concise and potentially faster than traditional function definitions.fn2
): This option uses a traditional function definition, which may be slower due to overhead from the function
keyword.Pros and Cons
Arrow Function (fn1
):
Traditional Function (fn2
):
Library and Special JS Features
There are no libraries used in this benchmark. However, it's worth noting that arrow functions were introduced in ES6 (ECMAScript 2015), which means they might not be supported in older browsers or environments.
Other Alternatives
If you're interested in exploring other alternatives for creating small, one-time-use functions, you could consider:
In summary, the benchmark compares the performance of two common ways to define small functions in JavaScript: arrow functions and traditional function definitions. The choice between these options depends on your specific use case, target audience, and desired level of conciseness.