var acc = 0;
for (var i = 0; i < 10000; i++) {
let value = 1;
{
let value = i;
acc += value;
};
value++;
if(value !== 2) throw 'error';
}
var acc = 0;
for (var i = 0; i < 10000; i++) {
let value = 1;
(value => acc += value)(i);
value++;
if(value !== 2) throw 'error';
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Brackets | |
Arrow function |
Test name | Executions per second |
---|---|
Brackets | 119585.3 Ops/sec |
Arrow function | 47567.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and considered.
What is being tested?
The benchmark compares the performance of two approaches:
The test cases measure the performance of each approach in a simple loop where a variable acc
is accumulated, and a condition is checked.
Options compared:
var acc = 0; for (var i = 0; i < 10000; i++) { let value = 1; { let value = i; acc += value; } value++; if(value !== 2) throw 'error'; }
(value => acc += value)(i);
(using the Fat Arrow syntax)Pros and Cons of each approach:
Library/Functionality used:
None mentioned. The test cases only involve JavaScript syntax.
Special JS feature/syntax:
(value => acc += value)(i);
) is used in the benchmark. This syntax was introduced in ECMAScript 2015 and allows for concise function expressions with a shorter syntax.Other alternatives:
If arrow functions are not an option, other alternatives could include:
function
keyword (e.g., function(value) { return acc + value; }
)(function(value) { return acc += value; })(i);
)It's worth noting that the choice of syntax often depends on personal preference, project requirements, and team norms.