<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(', ');
[new Set(testArr)]
_.uniq(testArr)
const { resultArray } = testArr.reduce((result, item) => {
if(!result.resultMap[item]){
result.resultMap[item] = true;
result.resultArray.push(item)
}
return result;
}, {resultArray: [], resultMap: {}});
return resultArray;
const resultArray = [];
const resultMap = {};
for(let i=0; i < testArr.length; i++ ) {
const item = testArr[i];
if(resultMap[item]){
continue;
}
resultMap[item] = true;
resultArray.push(item)
}
return resultArray;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array from Set | |
Lodash Uniq | |
Array Unique | |
For Loop |
Test name | Executions per second |
---|---|
Array from Set | 740219.4 Ops/sec |
Lodash Uniq | 335367.7 Ops/sec |
Array Unique | 858119.2 Ops/sec |
For Loop | 771541.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Overview
The benchmark compares four approaches to create an array of unique elements from a given string:
new Set()
uniq()
functionfor
loop with a resultMap
objectfor
loop without a resultMap
objectOptions Compared
Here are the options being compared, along with their pros and cons:
new Set()
: This approach uses JavaScript's built-in Set
data structure to create an array of unique elements.uniq()
function: This approach uses a dedicated library to create an array of unique elements.for
loop with resultMap
object: This approach uses a manual implementation to create an array of unique elements.for
loop without resultMap
object: This approach is similar to the previous one but lacks the resultMap
object for filtering duplicates.Library Used
Lodash's uniq()
function is used in one of the benchmark test cases. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object creation, and more.
Special JS Feature or Syntax
None of the benchmark test cases use any special JavaScript features or syntax beyond the standard language features provided by ES6 and later versions.
Other Alternatives
If you need to create an array of unique elements from a string in your own project, here are some alternative approaches:
Map
object with has()
method: Similar to the for
loop approach, but uses a Map
object for filtering duplicates.filter()
method with a callback function: Another manual implementation that uses the filter()
method to create an array of unique elements.// Example using Map object
const resultArray = [];
const resultMap = new Map();
for (const item of testArr) {
if (!resultMap.has(item)) {
resultMap.set(item, true);
resultArray.push(item);
}
}
// Example using filter() method
const resultArray = testArr.filter((item, index, self) => {
return self.indexOf(item) === index;
});
These approaches can be more efficient and concise than the for
loop approach, but may require more careful implementation to avoid errors.