Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs plain vanilla js performance on countby
(version: 0)
Comparing performance of:
vanilla js foreach Iter using set vs lodash countBy
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:
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:
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):
I'll break down the provided JSON and explain what's being tested, compared, and the pros and cons of different approaches. **Benchmark Definition** The benchmark measures the performance of two approaches to count unique categories across products: 1. **Vanilla JS:** * Uses a `for` loop with `Object.keys()` to iterate over product keys. * Checks if a category is present in the `uniqueCategoriesPresentAcrossProducts` Set using the `has()` method. * If present, adds the category to the Set; otherwise, skips it. 2. **Lodash:** * Uses the `_countBy()` function from Lodash to count unique categories across products. * The result is an object with category keys and their corresponding counts. **Options Compared** The benchmark compares two approaches: 1. Vanilla JS (without using a library) 2. Lodash (with its `countBy` function) **Pros and Cons:** 1. **Vanilla JS:** * Pros: + No external dependencies. + Can be optimized for specific use cases. * Cons: + May require more manual iteration and handling of edge cases. 2. **Lodash:** * Pros: + Faster execution ( likely due to internal optimization). + Less boilerplate code required. * Cons: + External dependency on Lodash. + May not be optimized for specific use cases. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as: * String manipulation * Array and object manipulation * Functional programming utilities (e.g., `countBy`, `forEach`) In this benchmark, the `_countBy()` function from Lodash is used to count unique categories across products. The `_countBy()` function takes two arguments: an object and a key function that extracts the desired property from each object. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you wanted to compare other approaches, here are some alternatives: * Using `Array.prototype.reduce()` instead of `Object.keys().forEach()` * Using a library like Underscore.js (similar to Lodash) or Ramda * Implementing your own iteration and Set management logic Keep in mind that the performance difference between these approaches may vary depending on the specific use case, hardware, and software environment.
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?