Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs filter for unique properties in object
(version: 0)
Comparing performance of:
use Map vs use Filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const uniqueItems = {}; const arr = [{ place: "here", name: "x", other: "other stuff1" }, { place: "there", name: "x", other: "other stuff2" }, { place: "here", name: "y", other: "other stuff4" }, { place: "here", name: "z", other: "other stuff5" } ] function getUniqueListByFilter() { return arr.filter((element) => { if (uniqueItems[element.place]) { return false; } uniqueItems[element.place] = true; return true; }); } function getUniqueListByMap() { return [...new Map(arr.map(item => [item.place, item])).values()] }
Tests:
use Map
const arr1 = getUniqueListByMap()
use Filter
const arr2 = getUniqueListByFilter()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
use Map
use Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 121 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
use Map
1563119.8 Ops/sec
use Filter
4669771.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark tests two approaches to get a unique list of properties in an object: using `Array.prototype.filter()` and using `Array.prototype.map()` with a `Map` data structure. **Test Case 1: "use Filter"** This test case uses the `filter()` method to remove duplicate elements from the array. The idea is to iterate over each element, check if its place property already exists in the `uniqueItems` object, and if it does, return false (indicating that the element should be skipped). If not, add the place property to the `uniqueItems` object and return true. **Pros of using filter()** * Efficient use of memory: The `filter()` method only iterates over each element once. * Simple implementation: The logic is straightforward and easy to understand. **Cons of using filter()** * Performance overhead due to function call and conditional checks * Potential performance impact if the array is very large **Test Case 2: "use Map"** This test case uses `Array.prototype.map()` in conjunction with a `Map` data structure to get a unique list of properties. The idea is to create a map where each key is a unique place property and its value is true. **Pros of using Map()** * Fast lookups: The `map()` method allows for fast lookups, making it suitable for large datasets. * Efficient use of memory: The `Map` data structure only stores unique keys. **Cons of using Map()** * Additional memory allocation: Creating a `Map` instance requires extra memory allocation. * Potential performance impact due to hash table operations **Library and Special JavaScript Features** In this benchmark, the following libraries/libraries-like-data-structures are used: * `Array.prototype.filter()` and `Array.prototype.map()`: built-in JavaScript methods * `Map` data structure: a native JavaScript data structure There are no special JavaScript features or syntaxes used in this benchmark. **Other Alternatives** For comparison, other approaches to achieve the same goal could be: * Using `Set` data structure instead of `Map` * Using a different sorting algorithm (e.g., merge sort) * Implementing a custom solution using recursive functions However, these alternatives are not tested or implemented in this benchmark.
Related benchmarks:
filter.map vs reduce 2
Unique values big list
Reduce vs map with empty filter
flatMap vs filter + map
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?