Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique item in Array of objects: reduce vs for loop
(version: 2)
Comparing performance of:
Array Unique vs For Loop vs Array Unique - v2 vs For Loop - v2
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var testArr = []; for (var i = 0; i < 100000; i++) { testArr.push({ val: String(getRandomInt(1000)) }); }
Tests:
Array Unique
const { resultArray } = testArr.reduce((result, item) => { if(!result.resultMap[item.val]){ result.resultMap[item.val] = true; result.resultArray.push(item) } return result; }, {resultArray: [], resultMap: {}}); return resultArray;
For Loop
const resultArray = []; const resultMap = {}; for(let item of testArr) { if(!resultMap[item.val]){ resultMap[item.val] = true; resultArray.push(item) } } return resultArray;
Array Unique - v2
const resultObj = testArr.reduce((result, item) => { if(!result[item.val]){ result[item.val] = item; } return result; }, {}); const resultArray = Object.values(resultObj); return resultArray;
For Loop - v2
const resultObj = {}; for(let item of testArr) { if(!resultObj[item.val]){ resultObj[item.val] = item; } } const resultArray = Object.values(resultObj); return resultArray;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array Unique
For Loop
Array Unique - v2
For Loop - v2
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):
I'll do my best to break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for finding unique elements in an array of objects: `reduce()` with a custom mapping function versus a traditional `for` loop. **`reduce()` Approach** The first approach uses the `reduce()` method, which is a functional programming technique that applies a reduction function to each element in an array. In this case, the reduction function maps each object to a boolean value indicating whether it's a unique element or not. The resulting array of unique elements is then returned. **`for` Loop Approach** The second approach uses a traditional `for` loop to iterate over the array and check for uniqueness. For each element, it checks if a corresponding key exists in an object (`resultMap`) and updates the result array accordingly. **Pros and Cons** * **Reduce() Approach:** + Pros: - More concise and elegant code - Leverages functional programming principles + Cons: - May have performance overhead due to function calls and object lookups - Less intuitive for developers without prior experience with `reduce()` or functional programming * **`for` Loop Approach:** + Pros: - More familiar and accessible to developers without prior experience with functional programming - Can be more efficient in certain scenarios due to reduced overhead of function calls + Cons: - Longer and more verbose code - May lead to errors if not handled carefully **Library: Lodash** The benchmark uses the Lodash library, which provides a `reduce()` method and other utility functions. The `reduce()` method is used in the first approach to map each object to a boolean value indicating uniqueness. **Special JS Feature/Syntax** None of the approaches explicitly use any special JavaScript features or syntax beyond the standard language. **Other Considerations** * **Performance**: The benchmark measures performance in terms of executions per second, which may not be directly comparable between the two approaches. Other metrics like average time taken or number of iterations might provide a more accurate picture. * **Readability**: While readability is subjective, the `for` loop approach can be seen as less readable due to its verbosity and lack of abstraction. * **Code Reusability**: The `reduce()` approach reuses the same function logic for multiple use cases, while the `for` loop approach requires duplicating code. **Alternatives** Other approaches for finding unique elements in an array could include: 1. Using a hash table (e.g., JavaScript's built-in `Map`) to store and check for uniqueness. 2. Utilizing a data structure like a set or a multiset to eliminate duplicates. 3. Implementing a custom algorithm that leverages the strengths of each approach. Keep in mind that the choice of approach depends on the specific requirements, constraints, and performance characteristics of the use case.
Related benchmarks:
Labels
Unique Array: Lodash vs spread new Set vs reduce vs for - random data
Unique lodash vs vanilla
Lodash max vs JS Math.max (2022)
Comments
Confirm delete:
Do you really want to delete benchmark?