Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Remove duplicates from array of objects
(version: 0)
Comparing performance of:
Using indexOf vs Using lastIndexOf vs Using a Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push({value: Math.floor((Math.random() * 10) + 1)}); }
Tests:
Using indexOf
array.filter((item, index) => array.indexOf(item) != index);
Using lastIndexOf
array.filter((item, index) => array.lastIndexOf(item) != index);
Using a Set
[...new Set(array)]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using indexOf
Using lastIndexOf
Using a Set
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'd be happy to explain what's being tested in this benchmark. **Benchmark Purpose** The goal of this benchmark is to measure the performance of three different approaches for removing duplicates from an array of objects in JavaScript: 1. Using `indexOf` method 2. Using `lastIndexOf` method 3. Using a `Set` data structure **Approach 1: Using `indexOf` method** This approach uses the `indexOf` method to check if an element exists in the array. If the index of the element is not equal to its own value, it means the element already exists in the array and can be removed. Pros: * Simple and straightforward implementation * Easy to understand and maintain Cons: * This approach has a high overhead due to the repeated use of `indexOf` method for each iteration. It's also vulnerable to performance degradation if the array is large, since it needs to search through the entire array to find the element. * Not recommended for large datasets. **Approach 2: Using `lastIndexOf` method** This approach uses the `lastIndexOf` method to check if an element exists in the array. If the last index of the element is not equal to its own value, it means the element already exists in the array and can be removed. Pros: * Similar to using `indexOf`, but potentially faster since it searches from the end of the array * Easy to understand and maintain Cons: * This approach also has a high overhead due to repeated use of `lastIndexOf` method. It's not significantly faster than using `indexOf`. * Still vulnerable to performance degradation for large datasets. **Approach 3: Using a `Set` data structure** This approach uses the `Set` data structure to store unique elements from the array. This way, only one instance of each element is stored in the set, making it efficient for removing duplicates. Pros: * Highly efficient and scalable * Fast and reliable Cons: * Requires extra memory for storing the set * Can be complex to understand and implement, especially for developers without experience with data structures. **Library: `Set`** The `Set` object is a built-in JavaScript data structure that stores unique elements. It's implemented as a hash table, which allows it to efficiently look up and store elements. **Special JS feature/Syntax** None mentioned in this benchmark. **Other Alternatives** Other alternatives for removing duplicates from an array of objects include: * Using `Map` instead of `Set`, since it preserves the order of elements * Using a custom implementation using arrays, loops, and conditional statements * Using a library like Lodash or Underscore.js that provides built-in functions for set operations It's worth noting that these alternatives may have their own trade-offs in terms of performance, memory usage, and complexity.
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Labels
Array .push() vs .unshift() with random numbers
Set.has v.s Array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?