<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}];
data.filter((v, i, a) => a.findIndex(t => t.a === v.a) === i)
_.uniqBy(data, 'a');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
js | |
lodash |
Test name | Executions per second |
---|---|
js | 1625066.5 Ops/sec |
lodash | 447325.1 Ops/sec |
This benchmark tests two methods for finding unique values in an array of objects: a vanilla JavaScript approach and using the Lodash library's _.uniqBy
function.
Test Cases:
"js": This test case uses a filter method with a custom callback function to achieve uniqueness. The callback checks if the current value's a
property already exists in the array.
"lodash": This test case utilizes Lodash's _.uniqBy
function, which directly identifies unique values based on a specified property (in this case, 'a'). Lodash is a popular JavaScript library that provides utility functions for common tasks.
Other Considerations:
a
). Different data structures (e.g., arrays of strings, nested objects) might lead to varying performance results.Alternatives:
Let me know if you'd like me to elaborate on any of these points!