Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Array: Lodash vs spread new Set vs reduce vs for v2
(version: 2)
Comparing performance of:
Array from Set vs Lodash Uniq vs Array Unique vs For Loop
Created:
4 years ago
by:
Registered User
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 Preparation code:
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(', ');
Tests:
Array from Set
[...new Set(testArr)]
Lodash Uniq
_.uniq(testArr)
Array Unique
const { resultArray } = testArr.reduce((result, item) => { if(!result.resultMap[item]){ result.resultMap[item] = true; result.resultArray.push(item) } return result; }, {resultArray: [], resultMap: {}}); return resultArray;
For Loop
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;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array from Set
Lodash Uniq
Array Unique
For Loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
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
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. Using `new Set()` 2. Using Lodash's `uniq()` function 3. Using a `for` loop with a `resultMap` object 4. Using a `for` loop without a `resultMap` object **Options Compared** Here are the options being compared, along with their pros and cons: 1. **`new Set()`**: This approach uses JavaScript's built-in `Set` data structure to create an array of unique elements. * Pros: Fast, efficient, and concise. * Cons: May not work as expected if the input string contains non-unique elements that are considered equal (e.g., "a" and "A"). 2. **Lodash's `uniq()` function**: This approach uses a dedicated library to create an array of unique elements. * Pros: Robust, well-tested, and easy to use. * Cons: Adds external dependencies and may not be suitable for small projects or those with strict licensing requirements. 3. **`for` loop with `resultMap` object**: This approach uses a manual implementation to create an array of unique elements. * Pros: Customizable, no external dependencies required. * Cons: More verbose, prone to errors if not implemented correctly. 4. **`for` loop without `resultMap` object**: This approach is similar to the previous one but lacks the `resultMap` object for filtering duplicates. * Pros: Simple, fast. * Cons: Prone to errors, may produce incorrect results if not implemented correctly. **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: * Using `Map` object with `has()` method: Similar to the `for` loop approach, but uses a `Map` object for filtering duplicates. * Using `filter()` method with a callback function: Another manual implementation that uses the `filter()` method to create an array of unique elements. ```javascript // 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); } } ``` ```javascript // 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.
Related benchmarks:
Unique Array: Lodash or from Set
Unique Array: Lodash or from Set with Clean and Sort
Unique Array: Lodash or spread new Set
lodash uniq vs Array.from(new Set()) vs spread new Set() long arrays
Comments
Confirm delete:
Do you really want to delete benchmark?