Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
count the number of occurrences in array
(version: 0)
Count the number of times each item appears in the array
Comparing performance of:
Using an object to hold the counts vs Using reduce
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
Tests:
Using an object to hold the counts
const counts = {}; for (const num of arr) { counts[num] = counts[num] ? counts[num] + 1 : 1; }
Using reduce
arr.reduce(function (acc, curr) { return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using an object to hold the counts
Using reduce
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):
**What is tested on the provided JSON?** The provided JSON represents two benchmark test cases for measuring the performance of JavaScript code that counts the occurrences of each item in an array. The tests compare two approaches: using an object to hold the counts and using the `reduce` method. **Options compared** 1. **Using an object to hold the counts**: This approach involves creating an empty object `{}` and then iterating through the array, incrementing the count for each number found in the array. If the number is not already a key in the object, it adds it with a value of 1. 2. **Using `reduce`**: This approach uses the `reduce` method to iterate through the array and accumulate the counts. It takes an initial value `{}` as the accumulator, which is then updated for each iteration. **Pros and Cons** * **Using an object to hold the counts**: + Pros: Simple, easy to understand, and works well for small arrays. + Cons: May not be efficient for large arrays due to the overhead of creating and updating the object. * **Using `reduce`**: + Pros: Efficient for large arrays, as it avoids the overhead of creating and updating an object. It also allows for a more concise code implementation. + Cons: May be less intuitive or harder to understand for developers who are not familiar with `reduce`. **Library** There is no explicit library mentioned in the provided JSON. However, the use of `reduce` suggests that the developer is familiar with this JavaScript method. **Special JS feature/syntax** The benchmark uses the syntax of JavaScript loops (e.g., `for...of`, `const counts[num] = ...`) and the `reduce` method, which are standard features in modern JavaScript. There are no special or experimental syntax features used in this benchmark. **Other alternatives** Alternative approaches to counting occurrences in an array might include: * Using a `Map` object instead of an object * Using a different data structure, such as a `Set` * Utilizing web workers or other concurrency models for parallel processing * Employing specialized libraries or frameworks that optimize array operations Keep in mind that the performance difference between these alternatives may be significant, and the choice of approach will depend on the specific requirements and constraints of the project.
Related benchmarks:
using .length within and out of for loop
unique elements in array using filter v2
unique elements in array using filter v2.3
unique elements in array using filter - large array
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?