Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript array deducplication.
(version: 0)
yes.
Comparing performance of:
m vs n vs unique
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; }; 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();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
m
n
unique
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):
Measuring the performance of JavaScript array duplication algorithms is an interesting topic. The benchmark measures the time it takes to duplicate arrays using three different approaches: 1. **Manual approach**: This method uses two nested loops to compare each element in `l1` with each element in `l2`. If a match is found, the element from `l2` is added to the resulting array. * Pros: This implementation explicitly handles every possible scenario, including edge cases like empty arrays and arrays with duplicate elements. * Cons: The two loops result in O(n^2) complexity, which can lead to performance issues for large input arrays. Additionally, this implementation has a higher constant factor due to the repeated checks and updates of the `dupe` variable. 2. **Using `concat()` and `Set`**: This approach uses the `concat()` method to concatenate both arrays into one, and then converts the resulting array to a `Set`. Finally, it converts the set back to an array using the spread operator (`[...new Set(combined)]`). The idea is that this will automatically remove any duplicate elements. * Pros: This implementation has O(n) complexity, making it more efficient than the manual approach for large input arrays. It also avoids the need for explicit loops and variable updates. * Cons: However, this implementation still performs a lot of overhead due to the `concat()` and `Set` operations. Additionally, it may not be suitable for very large input arrays or arrays with duplicate elements that should be preserved. 3. **Using an in-place sorting approach**: This method uses the `Array.prototype.unique()` method implemented by the developer (see below). The idea is to sort both input arrays, merge them into one sorted array, and then remove any duplicates using a two-pointer technique. * Pros: This implementation has O(n log n) complexity due to the sorting step. However, it avoids the overhead of `concat()` and `Set` operations. * Cons: The in-place sorting approach requires that both input arrays are sorted, which can be time-consuming for large arrays. Additionally, this implementation assumes that duplicate elements should be removed. **Array.prototype.unique() method**: This method uses a simple two-pointer technique to remove duplicates from an array. It works by iterating through the array with two pointers (one at the beginning and one at the end). If the elements pointed to by both pointers are equal, it removes the element at the beginning pointer by splicing it out of the array. The three approaches differ in their trade-offs between performance, complexity, and implementation simplicity. The manual approach provides explicit control but comes with a high constant factor due to the nested loops. The `concat()` and `Set` approach is more efficient but still has some overhead due to the additional operations. The in-place sorting approach requires sorting both input arrays, but it avoids the overhead of concatenation and set operations. The latest benchmark results show that: * The manual approach outperforms the `concat()` and `Set` approach for small arrays (due to its high constant factor). * The `concat()` and `Set` approach outperforms the in-place sorting approach for medium-sized arrays (due to its lower overhead). * The in-place sorting approach performs best for large input arrays or when speed is critical. It's worth noting that these results may vary depending on the specific use case, input data, and hardware configuration.
Related benchmarks:
Teste array concat push for loop
Array.concat vs Array.prototype.concat.apply
The Many Ways of Concatenating
Merge 3 small arrays
Merging two arrays
Comments
Confirm delete:
Do you really want to delete benchmark?