<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var testArr = "JRQ, SCTS, KKC, KKC, KKC, RGC, PDA, PDA, RGC, KKC, PDA, PDA, PDA, SCTS, KKC, DAH, JRM, JRM, RGC, RGC, DAMB, DAMB, KKC, SCTS, RGC, PAC, KKC, KKC, SCTS, SCTS, SCTS, SCTS, KKC, KKC, MIDK, MIDK, KKC, RGC, CHTS, CHTS, CHTS, CHTS, JRQ, JRQ, SCTS, DAH, DAH, AEWC, DQQL, PAC, DQQL, DQQL, AEWC, KIS, AEWC, DQQL, AEWC, PAC, RHN, RHN, KIS, KIS, RHN, RHN, RHN, KIS, KIS, KIS, KIS, KIS, JRQ, RGC, SCTS, RGC, SCTS, KKC, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, SCTS, DGJ, DGJ, RGC, KKC, KKC, MIDK, MIDK, RGC, KKC, RGC, RGC, RGC, RGC, DGJ, DGJ, PDA, RGC, JRQ, PDA, SCTS, RGC, RGC, SCTS, DXJM, JRQ, PDA, PDA, PDA, SCTS, DAH, DAH, RGC, RGC,".split(', ');
Array.from( new Set( testArr ) )
_.uniq(testArr)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array from Set | |
Lodash Uniq |
Test name | Executions per second |
---|---|
Array from Set | 249657.8 Ops/sec |
Lodash Uniq | 115223.2 Ops/sec |
The provided benchmark measures the performance of two different approaches to convert an array into a set and back into an array.
Approach 1: Using Array.from() and Set()
This approach uses the built-in Set
object in JavaScript to create a set from the array, and then converts it back into an array using the Array.from()
method. This is a straightforward and efficient way to achieve this conversion.
Approach 2: Using Lodash's uniq() function
This approach uses the uniq
function from the Lodash library, which is a popular utility library for JavaScript. The uniq
function takes an array as input and returns a new array with duplicate elements removed. This approach requires loading the Lodash library in the test environment.
Pros and Cons of each approach:
Set
.Set
-based approach.Library usage:
In this benchmark, Lodash is used to provide the uniq
function. The Lodash library provides a range of utility functions for tasks such as data manipulation, string manipulation, and more.
Special JS feature or syntax:
None of the approaches in this benchmark rely on any special JavaScript features or syntax. However, it's worth noting that the use of Set
objects is a relatively modern feature in JavaScript, introduced with ECMAScript 2015 (ES6).
Alternatives:
Other alternatives for converting an array into a set and back into an array might include:
collections
or arrays
from other utility libraries (e.g., Ramda, Immutable.js).Map
, to create a set-like data structure.However, these alternatives may not be as efficient or reliable as the native Set
-based approach, especially when it comes to performance and browser compatibility.