<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js</script>
var data = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}, {a: 6}, {a: 7}, {a: 8}, {a: 1}];
_.uniqBy(data, 'a');
const uniqueArray = a => [new Set(a.map(o => JSON.stringify(o)))].map(s => JSON.parse(s));
uniqueArray(data)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
javascript |
Test name | Executions per second |
---|---|
lodash | 10265506.0 Ops/sec |
javascript | 835110.5 Ops/sec |
Let's break down the provided JSON benchmark definition and explain what is tested, compared, and discussed.
Benchmark Definition
The benchmark tests two approaches to removing duplicates from an array while preserving order:
uniqBy
function, which removes duplicate elements based on a specified key (in this case, 'a'
). Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more.Array.prototype.map()
to create a new array with duplicate elements removed.Comparison
The benchmark compares the performance of these two approaches:
uniqBy
functionPros and Cons
Lodash (uniqBy
)
Pros:
Cons:
Custom JavaScript Implementation
Pros:
Cons:
Other Considerations
The benchmark also considers factors such as device platform (Desktop), operating system (Mac OS X 10.15.7), browser version (Chrome 112), and executions per second.
Library - Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more. Its uniqBy
function is specifically designed to remove duplicate elements based on a specified key, making it an attractive choice for this benchmark.
Special JS Feature/Syntax (None)
There are no special JavaScript features or syntax used in this benchmark.
Alternatives
Some alternatives to Lodash's uniqBy
function include:
Set
data structure in combination with Array.prototype.filter()
and JSON.stringify()
.lodash-es
(ES6+ version of Lodash) or lodash-clampMapKeys
for more fine-grained control over the key function.Keep in mind that these alternatives may have their own pros and cons, depending on the specific requirements of your use case.