Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript array deducplication 2.03
(version: 0)
electric boogaloo
Comparing performance of:
forEach Test vs usingSet Test vs For each 2 test
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 5, 4]; var b = [6, 8, 2, 7, 6, 1, 4, 3]; function usingForEach(l1, l2) { let u = [] l1.forEach(function(item) { if (u.indexOf(item) < 0) u.push(item); }); l2.forEach(function(item) { if (u.indexOf(item) < 0) u.push(item); }); return u; }; function usingForEach2(l1, l2) { let u = [] l1.forEach(function(item) { if (u.indexOf(item) == -1) u.push(item); }); l2.forEach(function(item) { if (u.indexOf(item) == -1) u.push(item); }); return u; }; function usingSet(l1, l2) { let u = l1.concat(l2); // Joins the l1 and l2 arrays together using concatenation. return [...new Set(u)]; // Array is converted into a 'Set' in order to remove duplicates, then is converted back into an array using the spread operator. };
Tests:
forEach Test
var test1 = usingForEach(a,b);
usingSet Test
var test2 = usingSet(a,b);
For each 2 test
var test1 = usingForEach2(a,b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach Test
usingSet Test
For each 2 test
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 benchmark and explain what's being tested. **Benchmark Definition:** The benchmark is designed to test three different approaches for removing duplicates from two arrays using JavaScript: 1. `usingForEach` 2. `usingForEach2` (a variation of the first approach) 3. `usingSet` The benchmark creates two sample arrays, `a` and `b`, and then defines three functions: * `usingForEach`: uses the `forEach` method to iterate over both arrays and push unique elements into a new array. * `usingForEach2`: uses the same approach as `usingForEach`, but with a subtle difference: it checks for equality using `== -1` instead of `< 0`. * `usingSet`: joins the two arrays together using concatenation and then converts the resulting array to a Set (a data structure that automatically removes duplicates) before converting it back to an array. **Options Compared:** The benchmark is comparing three different approaches: 1. **Using `forEach`**: This approach uses the `forEach` method to iterate over both arrays and push unique elements into a new array. 2. **Using `forEach2`**: This approach is similar to `usingForEach`, but with a different equality check (`== -1` instead of `< 0`). This variation may have slightly better performance due to less overhead in the browser's internal `indexOf` method. 3. **Using Set**: This approach joins the two arrays together using concatenation and then converts the resulting array to a Set, which automatically removes duplicates. **Pros and Cons:** Here are some pros and cons of each approach: * **Using `forEach`**: + Pros: Simple and straightforward implementation. + Cons: May have slower performance due to the browser's internal `indexOf` method being called on each iteration. * **Using `forEach2`**: + Pros: May have slightly better performance than `usingForEach` due to less overhead in the browser's internal `indexOf` method. + Cons: Uses a different equality check, which may not be consistent with other browsers or platforms. * **Using Set**: + Pros: Fast and efficient, as Sets automatically remove duplicates. + Cons: May require more memory to store the resulting Set. **Other Considerations:** * The benchmark does not consider issues like array indexing, out-of-bounds access, or potential null or undefined values in the input arrays. * It also assumes that the input arrays are not extremely large (thousands of elements), as performance degradation may occur with very large inputs. **Alternatives:** If you're interested in exploring alternative approaches, here are a few: 1. **Using `Array.prototype.filter`**: Instead of using `forEach`, you can use the `filter` method to create a new array with only unique elements. 2. **Using `Array.prototype.reduce`**: You can use the `reduce` method to accumulate unique elements into an array. 3. **Using a library like Lodash's `uniqBy` function**: If you're working in a Node.js environment or need more advanced filtering capabilities, consider using a library like Lodash. Keep in mind that these alternatives may have different performance characteristics and require additional setup or dependencies.
Related benchmarks:
For Each comparison
test124578
map vs for...of vs for
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Push vs Spread vs Double loop Ultimate
Comments
Confirm delete:
Do you really want to delete benchmark?