Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test-974
(version: 0)
Comparing performance of:
filter map vs reduce
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const TYPE_AGENCE_ABBR = 'agence'; const TYPE_CONSTRUCTEUR_ABBR = 'constructor'; var menu = [{ label: 'Personnaliser votre vitrine', to: '/vitrine/apercu/', forTypes: [TYPE_AGENCE_ABBR, TYPE_CONSTRUCTEUR_ABBR], }, { label: 'Contenu de votre fiche agence', to: '/vitrine/fiche/', forTypes: [TYPE_AGENCE_ABBR, TYPE_CONSTRUCTEUR_ABBR], }, { label: 'Gérer les adresses des agences', to: '/vitrine/gerer/', forTypes: [TYPE_AGENCE_ABBR, TYPE_CONSTRUCTEUR_ABBR], }, { label: 'Créer vos modèles de maisons', to: '/vitrine/creer-modele/', forTypes: [TYPE_CONSTRUCTEUR_ABBR], }, { label: 'Vos modèles de maisons', to: '/vitrine/modeles/', forTypes: [TYPE_CONSTRUCTEUR_ABBR], }, ]
Tests:
filter map
menu.filter((tab) => tab.forTypes.some((type) => type === 'agence')).map((tab) => ({ title: tab.label, ...tab, active: tab.to === '', }))
reduce
menu.reduce((acc, { forTypes, ...tab }) => (forTypes.includes('some') ? [...acc, { title: tab.label, ...tab, active: tab.to === '', }] : acc ), [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter map
reduce
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 being tested. **Benchmark Definition:** The benchmark consists of two individual test cases, each with its own definition in JSON format. 1. `filter map`: ```json "Benchmark Definition": "menu.filter((tab) => tab.forTypes.some((type) => type === 'agence')).map((tab) => ({\r\n title: tab.label,\r\n ...tab,\r\n active: tab.to === '',\r\n }))" ``` This test case is testing the performance of the `filter` and `map` methods in JavaScript. The `menu` array contains objects with a `forTypes` property, which is an array of strings representing different types (e.g., 'agence' or 'constructor'). The test filters out the elements where `forTypes` includes 'agence', and then maps each remaining element to a new object with additional properties. 2. `reduce`: ```json "Benchmark Definition": "menu.reduce((acc, { forTypes, ...tab }) => (forTypes.includes('some')\r\n ? [...acc, {\r\n title: tab.label,\r\n ...tab,\r\n active: tab.to === '',\r\n }]\r\n : acc\r\n ), [])" ``` This test case is testing the performance of the `reduce` method in JavaScript. The `menu` array is being reduced, with each element processed as an object that has two properties: `forTypes` (an array of strings) and `tab` (the rest of the object). If `forTypes` includes 'some', a new object is created with additional properties and appended to the accumulator; otherwise, the accumulator remains unchanged. **Options compared:** In this benchmark, only two options are being compared: 1. **Filter + Map**: This option uses both `filter` and `map` methods to process the `menu` array. 2. **Reduce**: This option uses the `reduce` method to process the `menu` array. **Pros and Cons:** Here are some pros and cons of each approach: **Filter + Map:** Pros: * Easy to understand and implement * Flexible, as it allows for easy filtering and transformation of data Cons: * Can be slower due to the overhead of function calls and object creation **Reduce:** Pros: * Efficient, as it reduces the number of iterations required to process the data * Can be faster than filter + map for large datasets Cons: * Less intuitive for developers who are not familiar with reduce operations * May require more memory to store the accumulator **Other Considerations:** * The use of `some` in the filter condition can lead to slower performance if the array is very large, as it requires iterating over each element. * The use of spread operators (`...`) in both approaches can also introduce overhead. **Library or Special JS Feature:** There are no libraries or special JavaScript features explicitly mentioned in this benchmark. However, the use of `reduce` and `some` implies familiarity with functional programming concepts. **Alternatives:** Other alternatives to these two approaches could include: * Using a library like Lodash or Underscore.js that provides additional utility functions for filtering and mapping data. * Using a different data structure, such as an object graph, instead of an array. * Optimizing the performance of each approach by using caching, memoization, or other optimization techniques. Overall, this benchmark is testing the performance of two common JavaScript methods for processing arrays: `filter` + `map` and `reduce`. The results can help identify which approach is faster and more efficient for specific use cases.
Related benchmarks:
asdffg
Arrowfunc
Operators
lodash set and dot notation
alecs@telicent
Comments
Confirm delete:
Do you really want to delete benchmark?