options = [
{
description: "Pack Toit Ouvrant Panoramique Electrique",
group_name: "exterieur",
id_equipement: 98327,
option_flag: 1,
pack_content: [98319, 98216],
pack_flag: 1,
prix_ttc: 1400,
_id: "5bae447a09a33a0342646f9b"
},
{
description: "Rouge Ultimate",
group_name: "exterieur",
id_equipement: 98301,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 830,
_id: "5bae41b209a33a0342646e5e"
},
{
description: "Roue galette",
group_name: "securite",
id_equipement: 98265,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 100,
_id: "5bae446b09a33a0342646f98"
},
{
description: "Pack Toit Ouvrant Panoramique Electrique",
group_name: "exterieur",
id_equipement: 98327,
option_flag: 1,
pack_content: [98319, 98216],
pack_flag: 1,
prix_ttc: 1400,
_id: "5bae447a09a33a0342646f9b"
},
{
description: "Rouge Ultimate",
group_name: "exterieur",
id_equipement: 98301,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 830,
_id: "5bae41b209a33a0342646e5e"
},
{
description: "Roue galette",
group_name: "securite",
id_equipement: 98265,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 100,
_id: "5bae446b09a33a0342646f98"
},
{
description: "Pack Toit Ouvrant Panoramique Electrique",
group_name: "exterieur",
id_equipement: 98327,
option_flag: 1,
pack_content: [98319, 98216],
pack_flag: 1,
prix_ttc: 1400,
_id: "5bae447a09a33a0342646f9b"
},
{
description: "Rouge Ultimate",
group_name: "exterieur",
id_equipement: 98301,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 830,
_id: "5bae41b209a33a0342646e5e"
},
{
description: "Roue galette",
group_name: "securite",
id_equipement: 98265,
option_flag: 1,
pack_content: [],
pack_flag: 0,
prix_ttc: 100,
_id: "5bae446b09a33a0342646f98"
}
];
const final = options.map(ele => ele.description).filter((ele, i, arr) => arr.indexOf(ele) === i);
const final = options.reduce((acc, cur) => acc.some(x=> (x.description === cur.description)) ? acc : acc.concat(cur), []);
const toObj = {};
options.forEach((o) => toObj[o.description] = o);
const final = Object.values(toObj);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 | |
3 |
Test name | Executions per second |
---|---|
1 | 2967501.0 Ops/sec |
2 | 775440.7 Ops/sec |
3 | 1181437.4 Ops/sec |
Let's break down the provided benchmark definition and explain what is tested, compared options, their pros and cons, and other considerations.
Benchmark Definition
The benchmark measures the performance of three different approaches to filter an array of objects based on a specific property (description
).
map()
+ filter()
const final = options.map(ele => ele.description).filter((ele, i, arr) => arr.indexOf(ele) === i);
This approach uses the map()
method to create a new array of descriptions and then applies the filter()
method to remove duplicates.
reduce()
+ some()
const final = options.reduce((acc, cur) => acc.some(x=> (x.description === cur.description)) ? acc : acc.concat(cur), []);
This approach uses the reduce()
method to iterate through the array and accumulate a result. If any element has a matching description, it returns the accumulator; otherwise, it concatenates the current element.
forEach()
const toObj = {};
options.forEach((o) => toObj[o.description] = o);
const final = Object.values(toObj);
This approach creates an object with descriptions as keys and objects from the original array as values using forEach()
. Finally, it uses Object.values()
to extract the filtered array of objects.
Comparison
The benchmark compares the performance of these three approaches across different environments (browsers) on various devices (Desktop). The test results show which approach is faster on each device.
Pros and Cons
map()
+ `filter()``reduce()
+ some()
forEach()
Other Considerations
reduce()
+ some()
) appears to be the fastest approach in most cases, but its readability and maintainability may be compromised due to the use of some()
.forEach()
) has a moderate performance profile, making it a viable option for scenarios where readability is prioritized over raw speed.Keep in mind that benchmarking results are specific to this particular dataset and may not generalize to other use cases.