Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy performance 2
(version: 0)
Comparing performance of:
lodash uniqBy vs javascript vs javascript 2 vs javascript 3
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js</script>
Script Preparation code:
var data = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }, { a: 6 }, { a: 7 }, { a: 8 }, { a: 1 }];
Tests:
lodash uniqBy
_.uniqBy(data, 'a');
javascript
const sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : (b[key] > a[key] ? -1 : 0); }; const uniqBy = (arr, a) => { arr.sort(sortBy(a)); const arr1 = []; arr1.push(arr[0]); for (let index = 1; index < arr.length; index++) { if (arr[index-1] && arr[index -1].a !== arr[index].a) { arr1.push(arr[index]); } } return arr1; } uniqBy(data, 'a')
javascript 2
function uniqBy(arr, key) { let seen = new Set() return arr.filter(it => { let val = it[key] if (seen.has(val)) { return false } else { seen.add(val) return true } }) } uniqBy(data, 'a')
javascript 3
const uniqBy = (arr, predicate) => { const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate]; return [...arr.reduce((map, item) => { const key = (item === null || item === undefined) ? item : cb(item); map.has(key) || map.set(key, item); return map; }, new Map()).values()]; }; uniqBy(data, 'a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash uniqBy
javascript
javascript 2
javascript 3
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test for uniqueness by value using different approaches in JavaScript. **Overview of the Benchmark** The test compares four different implementations of unique element extraction from an array: 1. **Lodash `uniqBy`**: Uses the Lodash library to perform the extraction. 2. **Custom JavaScript implementation 1 (JavaScript)**: A simple recursive function that sorts the array and then iterates through it to extract unique elements. 3. **Custom JavaScript implementation 2 (JavaScript 2)**: A more efficient implementation using a `Set` data structure to keep track of seen values. 4. **Custom JavaScript implementation 3 (JavaScript 3)**: A concise implementation using a `Map` data structure and the `typeof` operator. **Options Compared** The benchmark tests four different approaches: 1. Lodash `uniqBy` 2. Custom JavaScript implementation 1 (JavaScript) 3. Custom JavaScript implementation 2 (JavaScript 2) 4. Custom JavaScript implementation 3 (JavaScript 3) **Pros and Cons of Each Approach** 1. **Lodash `uniqBy`**: * Pros: Efficient, well-maintained library, easy to use. * Cons: Requires an additional dependency, may have performance overhead due to library overhead. 2. **Custom JavaScript implementation 1 (JavaScript)**: * Pros: Lightweight, no dependencies required. * Cons: Less efficient than the other implementations, recursive function can be slow for large arrays. 3. **Custom JavaScript implementation 2 (JavaScript 2)**: * Pros: Efficient use of `Set` data structure, good performance for small to medium-sized arrays. * Cons: May not perform well for very large arrays due to `Set` limitations. 4. **Custom JavaScript implementation 3 (JavaScript 3)**: * Pros: Concise and easy to read, efficient use of `Map` data structure. * Cons: May be less intuitive for developers without prior experience with `Map` data structures. **Library Used** The Lodash library is used in the first implementation (`lodash uniqBy`). Lodash provides a robust set of functions for common tasks, including array manipulation and data transformation. In this case, the `uniqBy` function is used to extract unique elements from an array based on a specified key. **Special JS Feature or Syntax** The JavaScript 3 implementation uses a concise syntax with the `typeof` operator, which is not explicitly mentioned in the benchmark JSON but is common knowledge among JavaScript developers. The `Map` data structure is also used in this implementation, which may require prior experience with this concept to understand and appreciate. **Other Alternatives** Alternative approaches to unique element extraction could include: * Using a `Set` data structure directly on the array elements * Using a custom sorting algorithm that takes advantage of the uniqueness requirement * Using a different library or framework (e.g., React, Angular) for array manipulation However, the provided benchmark focuses on comparing four specific implementations, and these alternatives are not part of the benchmark.
Related benchmarks:
uniqBy performance
uniqBy vs stringify performance
uniqBy performance ttt
uniqBy performance lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?