Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
wrapped to original
(version: 1)
asd asdasd
Comparing performance of:
list vs set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateRecords(count) { const records = []; for (let i = 1; i <= count; i++) { // Генерация случайного original.id (от 1 до count/2 для появления дубликатов) const originalId = Math.floor(Math.random() * (count / 2)) + 1; records.push({ id: i, original: { id: originalId } }); } return records; } function getUniqueRecords (records) { const seenIds = []; const uniqueRecords = []; for (const record of records) { if (!seenIds.includes(record.original.id)) { seenIds.push(record.original.id); uniqueRecords.push(record); } } return uniqueRecords; } function getUniqueRecordsSet(records) { const seenIds = new Set(); const uniqueRecords = []; for (const record of records) { if (!seenIds.has(record.original.id)) { seenIds.add(record.original.id); uniqueRecords.push(record); } } return uniqueRecords; } var records = generateRecords(1000)
Tests:
list
getUniqueRecords(records)
set
getUniqueRecordsSet(records)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
list
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
list
26068.3 Ops/sec
set
55635.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark test compares two different approaches to extracting unique records from an array of objects in JavaScript. These approaches are: 1. **Using an Array (`getUniqueRecords(records)`)**: - **Description**: This function iterates through the input array of records and maintains a separate array (`seenIds`) to track which `original.id`s have already been encountered. For each record, if its `original.id` is not in `seenIds`, it adds the record to the output array (`uniqueRecords`) and marks the `original.id` as seen. - **Pros**: - Simplicity: The solution is straightforward and easy to understand for developers who are familiar with JavaScript basics. - **Cons**: - Time Complexity: The `includes` method used to check for the existence of an item in `seenIds` results in a performance hit, particularly as the size of the `seenIds` array grows. Its time complexity is O(n) for each check, leading to a potential overall time complexity of O(n²) for the entire function. 2. **Using a Set (`getUniqueRecordsSet(records)`)**: - **Description**: This function utilizes the `Set` object, which is a built-in JavaScript collection designed to store unique values. Here, `seenIds` is a Set where unique `original.id`s are stored. As the function iterates through `records`, it checks against the Set for existence, which has a constant time complexity O(1) on average for both insertions and lookups. If an `original.id` isn't present in the Set, it adds the record to `uniqueRecords` and includes the `original.id` in the Set. - **Pros**: - Performance: This method is generally more efficient than the array-based approach due to the optimized operations of Sets. The overall time complexity is O(n), making it suitable for larger datasets. - **Cons**: - Slightly More Complex: Developers not familiar with Sets may need to understand the concept and how they work in JavaScript. ### Benchmark Results In the test results, the `getUniqueRecordsSet(records)` approach (using a Set) achieved approximately **55,635 executions per second**, while the `getUniqueRecords(records)` approach (using an array) achieved about **26,068 executions per second**. This indicates a significant performance advantage for the Set-based approach, particularly as the data size increases. ### Other Considerations - **Performance**: For large datasets, the choice between these two methods can significantly impact the performance of the application. As such, understanding data structures like Sets can help developers write more efficient code. - **Readability vs. Performance**: While the array approach is more intuitive for many, when performance is a critical factor, it may be worth favoring the complexity of the Set approach. - **Potential Alternatives**: Other alternatives could include using libraries like Lodash, which provides utility functions like `_.uniqBy()` for unique value extraction. However, utilizing the native built-in features like Sets will often yield better performance while also eliminating reliance on external libraries. In conclusion, when dealing with the need to extract unique values from a collection in JavaScript, using Sets is generally a more effective approach for performance-heavy applications, while still being accessible to developers with even basic knowledge of JavaScript.
Related benchmarks:
Split string
anagram getHash
Клавиатура
BRACKETS_IN_MINUS_WORDS_PATTERN2
filter: startsWith vs includes
lodash vs arr
приведения к числу и строке в или выражении 3
а я думал создание итератора будет дороже
wrapped to original111
Comments
Confirm delete:
Do you really want to delete benchmark?