Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Methods to remove duplicates from array (fork)
(version: 0)
Comparing performance of:
Using indexOf vs Using lastIndexOf vs Using a Set
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push(Math.floor((Math.random() * 10) + 1)); }
Tests:
Using indexOf
array.filter((item, index) => array.indexOf(item) != index);
Using lastIndexOf
array.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], []);
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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The provided benchmark tests three different approaches to remove duplicates from an array: 1. Using `indexOf` (index-based method) 2. Using `lastIndexOf` (index-based method, but with a twist) 3. Using a Set data structure Each approach is implemented in a separate test case. **Options compared** We have three options being compared: * **Index-based methods**: These methods use the `indexOf` or `lastIndexOf` functions to find the index of each item in the array. The main difference between these two methods is that `lastIndexOf` returns the last index of the item, whereas `indexOf` returns the first index. * **Set data structure**: This approach uses a Set data structure to store unique items from the array. **Pros and Cons** Here are some pros and cons for each approach: * **Index-based methods (Using indexOf and Using lastIndexOf)**: + Pros: Simple to implement, widely supported by browsers. + Cons: May be slower than other approaches due to the need to iterate over the entire array. Also, `lastIndexOf` may not work as expected if the item is not found in the beginning of the array. * **Set data structure (Using a Set)**: + Pros: Fast and efficient, as it uses a hash table to store unique items. + Cons: Requires additional memory to store the Set data structure, which may be a concern for very large arrays. **Library and purpose** In this benchmark, we can see that two libraries/libraries-like-technologies are used: * None of these are explicitly mentioned in the json. However, since one of the tests is using a "Set", a similar concept exists: `Set` (ECMAScript 2015) - which allows to create and store unique values. **Special JS feature or syntax** There is no explicit mention of any special JavaScript features or syntax being used in this benchmark. The implementation focuses on simple, standard JavaScript methods. **Other alternatives** In general, other approaches to remove duplicates from an array include: * Using a `Map` data structure (similar to a Set, but with additional capabilities) * Using a custom sorting algorithm that removes duplicates * Using a library like Lodash or Underscore.js, which provide various functions for removing duplicates. However, these alternatives are not being tested in this specific benchmark. In summary, the MeasureThat.net benchmark tests three approaches to remove duplicates from an array: index-based methods using `indexOf` and `lastIndexOf`, and a Set data structure. Each approach has its pros and cons, and we can see that the Set data structure is the fastest and most efficient option in this particular test case.
Related benchmarks:
The Non Repeating Number
Methods to remove duplicates from array
Methods to remove duplicates from array (test)
Methods to remove duplicates from array x2
Comments
Confirm delete:
Do you really want to delete benchmark?