Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Js Unique
(version: 0)
Test Unique across a large and small array
Comparing performance of:
jQuery vs Lodash vs Flags vs JS Func vs Set
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
Script Preparation code:
var $ = window.$; var _ = window._; var myArr = [1, 2, 1, 3, 1, 4]; var longArr = []; for (let i = 0; i < 100000; i++) { longArr.push(Math.floor(Math.random() * 100)); }
Tests:
jQuery
var result = $.unique( myArr ); var result2 = $.unique( longArr );
Lodash
var result = _.uniq( myArr ); var result2 = _.uniq( longArr );
Flags
var flags = [], output = [], l = myArr.length, i; for (i = 0; i < l; i++) { if (flags[myArr[i]]) continue; flags[myArr[i]] = true; output.push(myArr[i]); } var flags2 = [], output2 = [], l2 = myArr.length, i2; for (i2 = 0; i2 < l2; i2++) { if (flags2[longArr[i2]]) continue; flags2[longArr[i2]] = true; output2.push(longArr[i2]); }
JS Func
var unique = myArr.filter(function (itm, i, a) { return i == myArr.indexOf(itm); }); var unique2 = longArr.filter(function (itm, i, a) { return i == longArr.indexOf(itm); });
Set
var unique = [...(new Set(myArr))]; var unique2 = [...(new Set(longArr))];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
jQuery
Lodash
Flags
JS Func
Set
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):
The provided JSON represents a benchmarking test for uniqueness in arrays, specifically the ability to remove duplicates from an array while preserving order. **Options Compared:** 1. **jQuery**: Uses jQuery's `unique()` function to find unique elements in an array. 2. **Lodash**: Utilizes Lodash's `uniq` function to achieve similar results. 3. **Flags**: Employs a manual approach using flags to identify and exclude duplicate elements from the array. 4. **JS Func**: Leverages JavaScript's built-in `filter()` method with a custom function to remove duplicates, as well as a simple array spread operator (`...(new Set())`) to find unique elements. 5. **Set**: Uses the JavaScript `Set` data structure to find unique elements in an array. **Pros and Cons:** 1. **jQuery**: * Pros: jQuery is widely supported, and its `unique()` function can be easily integrated into existing projects. * Cons: Adds extra library size and potential overhead due to its complexity. 2. **Lodash**: * Pros: Lodash provides a lightweight and modular solution for various utility functions, including uniqueness. * Cons: Requires an additional library download and setup. 3. **Flags**: * Pros: Manually implemented approach allows for fine-grained control over the filtering process. * Cons: More prone to errors due to its custom implementation, and can be slower compared to other approaches. 4. **JS Func**: * Pros: Utilizes built-in JavaScript functions, reducing library dependencies and potential overhead. * Cons: Requires a basic understanding of JavaScript's `filter()` method and array spread operator. 5. **Set**: * Pros: Simple and efficient approach that leverages the Set data structure, making it suitable for large datasets. * Cons: May not preserve order or perform as well on very small arrays. **Library/Function Descriptions:** 1. **Lodash's `uniq()` function**: Removes duplicate elements from an array while preserving order, returning a new array with unique values. 2. **jQuery's `unique()` function**: A wrapper around Lodash's `uniq` function, providing a convenient interface for finding unique elements in arrays. **Special JavaScript Features/Syntax:** 1. The provided benchmarking test makes use of modern JavaScript features such as: * ES6-style arrow functions (`() => { }`) * Template literals (`\`${...}\``) * Array spread operator (`[...set]`) * `Set` data structure Please note that the array spread operator and Set data structure are supported in most modern browsers, but their behavior may vary across different environments. **Other Alternatives:** 1. **Array.prototype.reduce()**: An alternative approach to remove duplicates from an array using reduce method with a custom function. 2. **Filtering with `filter()` and checking for existence in the original array**: Another manual implementation using JavaScript's built-in `filter()` method with a custom callback function that checks if an element exists in the original array. Keep in mind that these alternatives might not be as efficient or concise as the provided benchmarking test, but can serve as useful reference points for further exploration.
Related benchmarks:
Lodash uniqBy vs Set 10000
Unique Array: Lodash vs spread new Set vs reduce vs for - random data
Unique lodash vs vanilla
_.uniqWith(arr, _.isEqual).length vs new Set(arr).size 1
New set vs UniqWith
Comments
Confirm delete:
Do you really want to delete benchmark?