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2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3,2,3,1,2,4,2,3,5,3]
_.uniq(elements)
[new Set(elements)]
elements.filter((v, i, a) => a.indexOf(v) === i)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.uniq | |
set | |
uniq by filter |
Test name | Executions per second |
---|---|
_.uniq | 141896.8 Ops/sec |
set | 142201.9 Ops/sec |
uniq by filter | 158149.8 Ops/sec |
The task is to create a benchmarking report for the _.uniq
function from Lodash, given an array of elements with some duplicates.
Benchmarking Report
Raw UA String: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Browser: Chrome 90
Device Platform: Desktop
Operating System: Mac OS X 10.15.7
Benchmark Results:
Test Name | Executions Per Second |
---|---|
_.uniq | 141896.796875 |
set | 142201.890625 |
uniq by filter | 158149.765625 |
Explanation:
_.uniq
function is being benchmarked with the array of elements provided.Conclusion:
The set-based approach is the fastest way to remove duplicates from an array in this benchmarking scenario. However, the performance differences between these three approaches are relatively small, and the choice of implementation will depend on specific use cases and requirements.