Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs plain vanilla js performance on countby vs foreach using set
(version: 0)
Comparing performance of:
lodash countBy vs vanilla js foreach Iter using set
Created:
3 years ago
by:
Guest
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<900; i++) { (i%2==0) ? products['product'+i] = {Category: 'category'+(i-1), Brand: 'brand'+(i-1)} : products['product'+i] = {Category: 'category'+i, Brand: 'brand'+i}; } var uniqueCategoriesPresentAcrossProducts = new Set(); var level = 'Category'; var count = 0;
Tests:
lodash countBy
_.countBy(products, level); Object.keys(products).length
vanilla js foreach Iter using set
Object.keys(products).forEach(product => 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
lodash countBy
vanilla js foreach Iter using set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash countBy
22816.0 Ops/sec
vanilla js foreach Iter using set
58397.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark definition and explanation. **What is being tested?** The test measures the performance of two approaches to count unique categories in an object: 1. **Lodash's `countBy` function**: This function groups objects by a specified key (in this case, `"Category"`). It returns an object where each key is a group and its value is an array of corresponding values. 2. **Vanilla JavaScript `forEach` iteration with a `Set` data structure**: This approach uses the `forEach` method to iterate over the object's keys and adds the corresponding category value to a `Set`. The `Set` automatically removes duplicates. **Options compared** The two options are compared in terms of their performance, measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons:** 1. **Lodash's `countBy` function**: * Pros: + Provides a concise and readable way to group objects. + Handles nested objects automatically (not shown in this benchmark). * Cons: + Adds an external dependency (the Lodash library), which may not be desirable for smaller projects or those with size constraints. 2. **Vanilla JavaScript `forEach` iteration with a `Set`**: * Pros: + No additional dependencies required. + Allows for fine-grained control over the iteration process. * Cons: + Requires manual management of the `Set`, including handling duplicates. **Library used:** In this benchmark, Lodash is used as a third-party library. It's a popular utility library that provides a wide range of functions for common tasks, such as data manipulation and object manipulation. **Special JS feature or syntax:** There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on the performance comparison between two different approaches to achieve a specific result. **Other alternatives:** If you're interested in exploring alternative approaches for counting unique categories, some other options could be: * Using `Array.prototype.reduce()` instead of `forEach` with a `Set`. * Utilizing a library like Moment.js or Dateutil for date-related calculations. * Implementing a custom solution using recursion or loops. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k corrected
lodash groupBy vs Array.reduce vs Array.group 100k
lodash groupBy vs Array.reduce 100k with array push
lodash groupBy vs Array.reduce 100k better
Comments
Confirm delete:
Do you really want to delete benchmark?