Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript array deducplication 2.02
(version: 0)
electric boogaloo
Comparing performance of:
forEach Test vs usingSet 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 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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach Test
usingSet 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 explanation of the provided benchmark. **Benchmark Definition** The benchmark is designed to measure the performance of two different approaches for removing duplicates from two arrays: `forEach` and `set`. The test case creates two sample arrays, `a` and `b`, and then defines two functions that use these arrays. One function uses the `forEach` method to iterate through both arrays and push unique items into a new array (`usingForEach`). The other function uses concatenation and then converting an array to a set using the spread operator to remove duplicates (`usingSet`). **Options Compared** The benchmark compares two options: 1. **Using `forEach`:** This approach iterates through both arrays, checks for uniqueness using `indexOf`, and pushes items into a new array if they don't exist. 2. **Using `set`:** This approach concatenates both arrays, converts the resulting array to a set, and then converts it back to an array using the spread operator. **Pros and Cons of Each Approach** 1. **`forEach`:** * Pros: + Can be more intuitive for developers familiar with iterating through arrays. + Does not require converting between different data structures (arrays to sets). * Cons: + May have performance issues due to the `indexOf` method being called repeatedly, leading to slower iterations. + Can lead to unnecessary re-iterations of the same elements if they appear in both arrays multiple times. 2. **`set`:** * Pros: + Generally faster and more efficient than using `forEach`, as it uses a hash-based data structure for set operations. + Less prone to re-executing iterations, making it more suitable for large datasets. * Cons: + Requires converting between arrays and sets, which can lead to additional overhead. + May require additional libraries or functions (like `Set` in JavaScript) that might not be available in all environments. **Library and Syntax** The benchmark uses the `Set` data structure from the JavaScript standard library. This is a built-in data structure that stores unique values, making it suitable for removing duplicates. There are no special JavaScript features or syntax used beyond what's already mentioned (using `forEach`, sets, etc.). **Alternatives** Other alternatives for removing duplicates from arrays include: 1. **Using `filter` and `includes`:** * Iterate through one array and use the other as a filter to exclude elements. 2. **Using `reduce`:** * Use the `reduce` method to iterate through both arrays, accumulating unique items into an accumulator array. Keep in mind that these alternatives might have varying performance characteristics compared to using `set`.
Related benchmarks:
Array remove item using Array.concat vs array spread
test spread vs concat
Spread vs Push3
lodash uniq vs set and Array.from
Set to Array vs. lodash uniq
Comments
Confirm delete:
Do you really want to delete benchmark?