Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript array deducplication 2
(version: 0)
electric boogaloo
Comparing performance of:
m vs n vs unique vs ForEACH
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function m(l1, l2) { var u = []; for (var i = 0; i < l1.length; i++) { u.push(l1[i]); } for (var j = 0; j < l2.length; j++) { var dupe = false; for (var i = 0; i < l1.length; i++) { if (l1[i] === l2[j]) { dupe = true; } } if (!dupe) { u.push(l2[j]); } } return u; } function n(l1, l2) { let combined = [].concat(l1, l2); return [...new Set(combined)]; } Array.prototype.unique = function() { var a = this.concat(); for(var i=0; i<a.length; ++i) { for(var j=i+1; j<a.length; ++j) { if(a[i] === a[j]) a.splice(j--, 1); } } return a; }; 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; } var a = [1, 2, 3, 4, 5]; var b = [3, 3, 4, 5, 5, 6, 7];
Tests:
m
var testM = m(a, b);
n
var testN = n(a, b);
unique
var testUnique = a.concat(b).unique();
ForEACH
var testY = usingForEach(a,b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
m
n
unique
ForEACH
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 break down the provided benchmark definition and options: **Benchmark Definition:** The benchmark is testing two main functions: 1. `m(l1, l2)`: This function creates an array `u` by pushing elements from `l1` into it and then checks if each element in `l2` already exists in `l1`. If it does, the element is not pushed into `u`. 2. `n(l1, l2)`: This function combines `l1` and `l2` into a single array using the spread operator (`...`) and then uses the `Set` data structure to remove duplicates. 3. `unique()`: This is a custom method that modifies the original array by removing duplicate elements. 4. `usingForEach(l1, l2)`: This function iterates over both arrays and checks if each element exists in the other array. If it does, the element is not pushed into the result array. **Options Compared:** The benchmark is comparing different approaches to remove duplicates from two arrays: * Method 1: `m(l1, l2)` * Method 2: `n(l1, l2)` * Method 3: `unique()` * Method 4: `usingForEach(l1, l2)` **Pros and Cons of Each Approach:** 1. **Method 1 (m)**: * Pros: Custom implementation allows for fine-grained control over duplicate removal. * Cons: More complex logic, may be slower due to explicit checks. 2. **Method 2 (n)**: * Pros: Uses built-in JavaScript features (`Set`) and spread operator, which are efficient and concise. * Cons: May not be suitable for large arrays due to memory limitations. 3. **Method 3 (unique())**: * Pros: Simple and easy to understand implementation using array modification methods. * Cons: Modifies the original array, may not be suitable for all use cases. 4. **Method 4 (usingForEach)**: * Pros: Custom implementation allows for fine-grained control over duplicate removal. * Cons: More complex logic, slower due to explicit checks. **Other Considerations:** * Memory usage: Methods that create new arrays or objects may consume more memory than those that modify existing data structures. * Performance: Methods with built-in JavaScript features tend to be faster due to optimization and caching. * Code readability: Simple and concise implementations are often easier to understand and maintain. **Alternatives:** Other approaches for removing duplicates from two arrays include: 1. Using the `filter()` method in combination with a callback function. 2. Utilizing libraries like Lodash or Ramda, which provide optimized duplicate removal functions. 3. Implementing a custom sorting and filtering approach using array methods like `sort()`, `indexOf()`, and `splice()`. Keep in mind that each approach has its trade-offs, and the best solution depends on specific requirements, performance constraints, and personal preference.
Related benchmarks:
Merge array of objects with an object
contains duplicate
for i versus map
Merging two arrays
Array remove
Comments
Confirm delete:
Do you really want to delete benchmark?