Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing findMode
(version: 3)
Comparing performance of:
findMode vs mostCommonNumber
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
// Purposely global numbers = Array.from({length: 1e6}, () => Math.floor(Math.random() * 1e20)) function findMode(numbers) { let counted = numbers.reduce((acc, curr) => { if (curr in acc) { acc[curr]++; } else { acc[curr] = 1; } return acc; }, {}); let mode = Object.keys(counted).reduce((a, b) => counted[a] > counted[b] ? a : b); return mode; } function mostCommonNumber(numbers) { let map = new Map() for (let num of numbers) { map.set(num, (map.get(num) || 0) + 1) } let mostCommonNumber = NaN let maxCount = -1 for (let [num, count] of map.entries()) { if (count > maxCount) { maxCount = count mostCommonNumber = num } } return mostCommonNumber }
Tests:
findMode
findMode(numbers)
mostCommonNumber
mostCommonNumber(numbers)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findMode
mostCommonNumber
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):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark consists of two test cases: `findMode` and `mostCommonNumber`. Both functions are designed to find the mode (the most frequently occurring value) in an array of numbers. The tests compare the performance of these two implementations. **Implementation Comparison** The two implementations differ in their approach: 1. **findMode**: This implementation uses a hash map (`Object.keys` and `reduce`) to count the occurrences of each number. It then finds the key with the highest value. 2. **mostCommonNumber**: This implementation uses an array-based approach, creating a frequency map using a `Map` object. It iterates through the numbers, incrementing the count for each unique number. **Pros and Cons** * **findMode**: + Pros: Simple and easy to understand. Uses built-in JavaScript features. + Cons: May have performance issues due to the use of `Object.keys`, which has a higher overhead than using an array-based approach. * **mostCommonNumber**: + Pros: Efficient for large datasets, as it only iterates through the numbers once. + Cons: Requires additional memory allocation and iteration. **Other Considerations** * **Browser Support**: The benchmark uses Chrome 61, which may not be supported by older browsers or versions of JavaScript. * **Device Platform**: The benchmark runs on a Windows device, which may affect performance due to factors like disk I/O and network bandwidth. * **Randomized Input**: The input data is randomly generated using `Array.from` and `Math.random`, which ensures that the results are not deterministic. **Special JS Features** In this benchmark, we don't see any special JavaScript features being used. However, some other benchmarks might use features like: * ES6 classes * async/await * Promises * Web Workers These features can introduce additional overhead or complexities, making it harder to compare performance results. **Alternative Implementations** If you're interested in exploring alternative implementations, here are a few examples: 1. **Sorting-based approach**: Instead of using a hash map or array-based approach, you could sort the numbers and find the most common value. This approach would have higher overhead due to sorting. 2. **Distributed computing**: You could divide the input data among multiple processes or workers and use a consensus algorithm to find the mode. This approach would require additional complexity and synchronization mechanisms. Keep in mind that these alternative implementations might not be as efficient or scalable as the original `findMode` and `mostCommonNumber` implementations.
Related benchmarks:
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Count Array Duplicates
Comments
Confirm delete:
Do you really want to delete benchmark?