Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs plain vanilla countBy on object iter
(version: 3)
Comparing performance of:
vanilla js foreach Iter using set vs lodash countBy
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var products = { product1 : {Category: 'category1', Brand: 'brand1'}, product2 : {Category: 'category1', Brand: 'brand2'}, product3 : {Category: 'category2', Brand: 'brand3'}, product4 : {Category: 'category2', Brand: 'brand3'} } for(var i=5; i<95; i++) { products['product'+i] = {Category: 'category3', Brand: 'brand'+i} } var uniqueCategoriesPresentAcrossProducts = new Set(); var level = 'Category'; var count = 0;
Tests:
vanilla js foreach Iter using set
_.countBy(products, level); Object.keys(products).length
lodash countBy
Object.keys(products).forEach(product => uniqueCategoriesPresentAcrossProducts.has(products[product][level]) ? null : uniqueCategoriesPresentAcrossProducts.add(products[product][level]) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
vanilla js foreach Iter using set
lodash countBy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
vanilla js foreach Iter using set
165603.0 Ops/sec
lodash countBy
455887.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark is designed to compare two approaches for counting unique categories present across products in an object: `lodash.countBy` and vanilla JavaScript `forEach` with a `Set`. **Test Case 1: Vanilla JS `forEach` Iter using Set** This test case uses a vanilla JavaScript approach to iterate over the product keys, check if each category is already in the `uniqueCategoriesPresentAcrossProducts` set, and add it if not. The iteration is done using a traditional `for` loop with an index variable. **Pros:** 1. **Easy to understand**: This implementation is straightforward and easy to comprehend for developers familiar with vanilla JavaScript. 2. **No external dependencies**: No external library or module needs to be included, which can reduce the risk of unnecessary dependencies affecting benchmark results. **Cons:** 1. **Performance overhead**: The use of a `for` loop and an additional check in the iteration may introduce performance overhead compared to other approaches. 2. **Limited parallelization**: This implementation is not well-suited for parallelization, as it relies on sequential iteration over the product keys. **Test Case 2: Lodash `countBy`** This test case uses the `lodash.countBy` function to count unique categories present across products in the object. **Pros:** 1. **Concise and expressive**: The `countBy` function is a compact and readable way to solve the problem. 2. **Optimized implementation**: Lodash provides an optimized implementation for this specific use case, which can lead to better performance compared to vanilla JavaScript approaches. **Cons:** 1. **External dependency**: This test case relies on including the Lodash library as a script in the HTML file, which can add unnecessary overhead and dependencies. 2. **Potential caching issues**: If the `countBy` function is not properly memoized or cached, it may lead to slower performance due to repeated recalculations. **Special Consideration: Set Data Structure** The test case uses a `Set` data structure to keep track of unique categories present across products. This choice is reasonable for this specific problem, as sets provide fast lookups and insertions. **Alternative Approaches** Other possible approaches for solving this benchmark could include: 1. **Using `Object.keys()` and `Array.prototype.filter()`**: A vanilla JavaScript approach that iterates over the product keys using `Object.keys()` and filters out duplicates using `Array.prototype.filter()`. 2. **Using `reduce()` or `forEach()` with an accumulator**: Both of these approaches can be used to iterate over the product keys and accumulate unique categories in a single pass. 3. **Using a library like `fast-callback` or `micro-ramp`**: These libraries provide optimized implementations for specific use cases, including counting unique values across objects. These alternative approaches may offer different trade-offs between performance, readability, and external dependencies compared to the Lodash `countBy` function and vanilla JavaScript `forEach` approach.
Related benchmarks:
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce [correct] 100k
lodash groupBy vs Array.reduce 100k corrected
lodash groupBy vs Array.reduce 100
lodash groupBy vs Array.reduce grouping
Comments
Confirm delete:
Do you really want to delete benchmark?