var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [];
for (i = 0; i < a.length; i++)
{
b.push({"identifier" : a[i]});
}
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [];
b = a.map(function(i){ return {"identifier": i}; });
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = [];
b = a.map(i => {return {'identifier': i}});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
map with basic function | |
map with fat arrow function |
Test name | Executions per second |
---|---|
for | 67590.4 Ops/sec |
map with basic function | 485257.5 Ops/sec |
map with fat arrow function | 484260.1 Ops/sec |
I'd be happy to explain what's being tested in the provided benchmark.
The test cases are designed to measure the performance of three different approaches for adding elements to an empty array using JavaScript. The approaches are:
for
loop to iterate over the elements of the original array and push them onto the new array.map()
method, which applies a provided function to each element in the original array and returns a new array with the results. In this case, the function simply creates an object with an "identifier" property set to the current element value.Pros and Cons:
map()
was introduced).Other considerations:
map()
method and the use of strings with double quotes ("identifier": i
).In terms of alternatives, other approaches could include:
push
and splice
methods, to avoid the overhead of creating a new array.However, it's worth noting that this specific benchmark is designed to measure the performance of JavaScript code, specifically the three approaches listed above.