Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript array deducplication 2.01
(version: 0)
electric boogaloo
Comparing performance of:
m vs n vs unique vs y
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){ 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();
y
var y = 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
y
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. **Benchmark Definition JSON Explanation** The provided JSON represents a benchmark definition for measuring the performance of JavaScript functions. It contains two main sections: 1. **Script Preparation Code**: This section defines three JavaScript functions: * `m(l1, l2)`: This function takes two arrays as input and returns a new array with duplicates removed. * `n(l1, l2)`: This function concatenates the input arrays and returns a new array with duplicates removed using the `Set` data structure. * `usingForEach(l1,l2)`: This function iterates over both input arrays and adds unique elements to a result array. * `Array.prototype.unique = ...`: This is a method that removes duplicates from an array in-place. However, it's not directly applicable to the benchmark as it modifies the original array. 2. **Html Preparation Code**: This section is empty, indicating that no HTML preparation code is required for this benchmark. **Options Compared** The benchmark compares four different approaches to remove duplicates from two input arrays: 1. `m(l1, l2)`: Uses a nested loop to iterate over both arrays and add elements to a result array. 2. `n(l1, l2)`: Concatenates the input arrays using the spread operator (`[...new Set(combined)]`) to remove duplicates. 3. `usingForEach(l1,l2)`: Iterates over both input arrays and adds unique elements to a result array using the `forEach` method. 4. `Array.prototype.unique = ...`: This is not directly applicable to the benchmark, but it's included for comparison purposes. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **m(l1, l2)**: * Pros: Simple implementation, easy to understand. * Cons: Has a high time complexity due to the nested loop. 2. **n(l1, l2)**: * Pros: Efficient use of built-in `Set` data structure, fast execution. * Cons: May not be as intuitive for developers unfamiliar with `Set`. 3. **usingForEach(l1,l2)**: * Pros: Easy to understand, simple implementation. * Cons: May have a higher time complexity due to the iteration over both arrays. 4. **Array.prototype.unique = ...**: * Pros: In-place modification, potentially faster execution. * Cons: Not directly applicable to this benchmark, modifies original array. **Library and Special JS Features** The `Set` data structure is used in the `n(l1, l2)` implementation. `Set` is a built-in JavaScript object that automatically removes duplicates from its elements. There are no special JavaScript features or syntax mentioned in the benchmark definition. **Other Alternatives** If you're interested in exploring alternative approaches to removing duplicates from arrays, here are a few options: 1. **Filter and Map**: Use the `filter` method to remove elements that don't match a condition, followed by the `map` method to create a new array with unique elements. 2. **Reduce**: Use the `reduce` method to iterate over an array and accumulate unique elements in a result array. 3. **Array.prototype.findIndex** and **Array.prototype.findIndexOrThrow**: Use these methods to find the index of an element in an array, then use that index to create a new array with unique elements. Keep in mind that each approach has its pros and cons, and the best choice depends on your specific use case and performance requirements.
Related benchmarks:
equiv arrays 3
Unique elements of two arrays
contains duplicate
for i versus map
Array remove
Comments
Confirm delete:
Do you really want to delete benchmark?