Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Merge array test 2
(version: 0)
Comparing performance of:
1 vs 2 vs 3
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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" } ];
Tests:
1
const final = options.map(ele => ele.description).filter((ele, i, arr) => arr.indexOf(ele) === i);
2
const final = options.reduce((acc, cur) => acc.some(x=> (x.description === cur.description)) ? acc : acc.concat(cur), []);
3
const toObj = {}; options.forEach((o) => toObj[o.description] = o); const final = Object.values(toObj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
1
2
3
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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`). 1. **Approach 1: `map()` + `filter()`** ```javascript 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. 2. **Approach 2: `reduce()` + `some()`** ```javascript 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. 3. **Approach 3: Object creation + `forEach()`** ```javascript 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** 1. **`map()` + `filter()``** * Pros: Simple, readable code; efficient memory usage. * Cons: May be slower due to the overhead of creating a new array. 2. **`reduce()` + `some()`** * Pros: Can be more efficient if the accumulator is reused; can handle larger arrays. * Cons: Code can be less readable; may require additional setup for accumulator initialization. 3. **Object creation + `forEach()`** * Pros: Easy to understand and implement; can be more efficient with large datasets. * Cons: May create unnecessary objects in memory; slower due to object creation. **Other Considerations** * The benchmark results show significant variations in performance across different browsers and devices, highlighting the importance of testing on diverse environments. * Approach 2 (`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()`. * Approach 3 (Object creation + `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.
Related benchmarks:
Array.map vs reduce
Merge array test
array some vs _.some III
Underscore pluck vs map 5
Comments
Confirm delete:
Do you really want to delete benchmark?