Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs flatMap v2
(version: 0)
Comparing performance of:
filter vs flatMap
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var objArray = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}, {id: 11}, {id: 12}, {id: 13}, {id: 14}, {id: 15}, {id: 16}, {id: 17}, {id: 18}, {id: 19}, {id: 20}]; var idArray = [1];
Tests:
filter
var filteredObjArray = objArray.filter(obj => idArray.includes(obj.id));
flatMap
var filteredObjArray = idArray.flatMap(id => objArray.find(obj => obj.id === id) || []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter
flatMap
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 and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare the performance of two approaches: filtering an array using `Array.prototype.filter()` and flattening an array using `Array.prototype.flatMap()`. The test case uses a JavaScript object array `objArray` containing 20 objects with an "id" property, as well as an array `idArray` containing a single value. **Script Preparation Code** The script preparation code defines the `objArray` and `idArray` variables, which are used in the benchmark. The `var idArray = [1];` line creates an array containing only one element, which is used to filter or map the objects in `objArray`. **Html Preparation Code** There is no HTML preparation code provided, so we can assume that this step is not relevant for this particular benchmark. **Individual Test Cases** The test cases are defined as separate benchmarks: 1. **filter**: This test case uses the `Array.prototype.filter()` method to create a new array containing only the objects in `objArray` whose "id" property matches one of the values in `idArray`. The condition used is `obj => idArray.includes(obj.id)`. 2. **flatMap**: This test case uses the `Array.prototype.flatMap()` method to create a new array containing the results of applying the function `(id => objArray.find(obj => obj.id === id) || [])` to each value in `idArray`. The purpose of this function is unclear, as it returns an empty array if no matching object is found. **Pros and Cons** Here's a brief analysis of the two approaches: **Filter (Array.prototype.filter())** Pros: * Simple and well-known algorithm * Works for most use cases Cons: * May create intermediate arrays or objects that need to be garbage collected * Can be slower than flatMap if the input array is large **FlatMap (Array.prototype.flatMap())** Pros: * Avoids creating intermediate arrays or objects, reducing memory allocation and garbage collection overhead * Returns a new array, avoiding side effects on the original data Cons: * Less well-known algorithm compared to filter() * May require additional checks for null/undefined values in the input array * Can be slower than filter() if the input array is small **Library Usage** In both test cases, no external libraries are used. However, it's worth noting that flatMap() was introduced in ECMAScript 2019 (ES10) as a part of the standard library. **Special JS Feature or Syntax** There are no special JavaScript features or syntax mentioned in the benchmark definition. Both `filter()` and `flatMap()` are built-in methods on arrays, and their usage is straightforward. **Other Alternatives** If you need to filter an array, other alternatives besides `Array.prototype.filter()` include: * Using a simple loop with conditional statements * Utilizing a library like Lodash's `filter` function If you need to flatten an array, other alternatives besides `Array.prototype.flatMap()` include: * Using a simple loop with nested iterations * Utilizing a library like Ramda's `map` and `flatten` functions Keep in mind that these alternatives may have different performance characteristics or require additional setup.
Related benchmarks:
flatMap() vs filter().map() - arrays
filter vs flatMap
flatMap vs filter + map
lodash flatten vs array.flatMap corrected transform
Comments
Confirm delete:
Do you really want to delete benchmark?