Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test sort uniq
(version: 0)
Comparing performance of:
slow vs new vs new2
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.min.js"></script>
Script Preparation code:
var data = [] function getRandomDate() { const startMillis = new Date('2020-06-02T00:00:00.000Z').getTime(); const endMillis = new Date().getTime(); const randomMillis = startMillis + Math.random() * (endMillis - startMillis); const randomDate = new Date(randomMillis); return randomDate; } for (var i = 0; i < 14000; i++) { data.push({ customerId: `CTM-${i % 10}`, worklistId: `WL-${i % 100}`, createdDateTime: getRandomDate() }); }
Tests:
slow
var result = R.uniqBy( R.props(['worklistId', 'customerId']), [...data].sort( (a, b) => new Date(a.createdDateTime).getTime() - new Date(b.createdDateTime).getTime(), ), )
new
var result = R.uniqWith( (a, b) => a.customerId === b.customerId && a.worklistId === b.worklistId, [...data].sort( (a, b) => new Date(a.createdDateTime).getTime() - new Date(b.createdDateTime).getTime(), ), )
new2
var result = R.uniqWith( (a, b) => a.customerId === b.customerId && a.worklistId === b.worklistId, data.sort( (a, b) => a.createdDateTime - b.createdDateTime, ), )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slow
new
new2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slow
3.1 Ops/sec
new
28.9 Ops/sec
new2
116.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided benchmark and its various options. **Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that measures the performance of three different approaches for removing duplicates from an array while preserving uniqueness. The input data is generated using a custom `getRandomDate` function, which creates 14,000 objects with unique customer IDs and worklist IDs. **Script Preparation Code** The script preparation code defines the input data as an empty array `data`. It also defines a utility function `getRandomDate` that generates random dates within a specific time range. This function is used to populate the input data with random created dates. **Html Preparation Code** The HTML preparation code includes a link to include the Ramda library, which provides functional programming utilities. **Benchmark Options** There are three benchmark options: ### 1. `R.uniqBy` (Slow) ```javascript var result = R.uniqBy( R.props(['worklistId', 'customerId']), [...data].sort((a, b) => new Date(a.createdDateTime).getTime() - new Date(b.createdDateTime).getTime()), ) ``` This option uses the `R.uniqBy` function from Ramda to remove duplicates based on a specific set of properties (`worklistId` and `customerId`). The input data is sorted in ascending order using a compare function that extracts the created date from each object. **Pros:** * Preserves uniqueness by comparing objects based on a specific set of properties. * Provides a clear and concise way to remove duplicates. **Cons:** * Requires sorting the input data, which can be computationally expensive for large datasets. ### 2. `R.uniqWith` (New) ```javascript var result = R.uniqWith( (a, b) => a.customerId === b.customerId && a.worklistId === b.worklistId, [...data].sort((a, b) => new Date(a.createdDateTime).getTime() - new Date(b.createdDateTime).getTime()), ) ``` This option uses the `R.uniqWith` function from Ramda to remove duplicates based on a custom comparison function. The input data is sorted in ascending order using the same compare function as before. **Pros:** * Preserves uniqueness by comparing objects based on a custom set of properties. * Provides more flexibility than `R.uniqBy`. **Cons:** * Requires defining a custom comparison function, which can be error-prone and less readable than using a specific set of properties. ### 3. In-place sorting (New2) ```javascript var result = R.uniqWith( (a, b) => a.customerId === b.customerId && a.worklistId === b.worklistId, data.sort((a, b) => a.createdDateTime - b.createdDateTime), ) ``` This option uses the `R.uniqWith` function from Ramda to remove duplicates while preserving the original order of objects in the input array. The input array is sorted in-place using a compare function that extracts the created date from each object. **Pros:** * Preserves uniqueness and the original order of objects. * Reduces memory usage by sorting in-place. **Cons:** * Requires modifying the original input array, which can be undesirable for some use cases. **Other Considerations** The benchmark only considers the performance of removing duplicates while preserving uniqueness. Other factors, such as memory allocation or caching effects, may also impact performance. In conclusion, each option has its pros and cons. The choice between them depends on the specific requirements and constraints of the application.
Related benchmarks:
Lodash min vs sort moment (lodash 4.7.11)
Lodash min vs sort string (lodash 4.7.11)
Lodash sort vs array.prototype.sort vs. sort by key
test sort uniq2
Comments
Confirm delete:
Do you really want to delete benchmark?