Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.countBy vs native code
(version: 0)
Lodash.countBy vs native code
Comparing performance of:
test1 vs test2
Created:
5 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:
const values = { featurePacks: { feature1: true, feature2: true, feature3: false, feature4: true, feature5: false, feature6: true, feature7: false, } }
Tests:
test1
const values = { featurePacks: { feature1: true, feature2: true, feature3: false, feature4: true, feature5: false, feature6: true, feature7: false, } } var tmp = Object.values(values?.featurePacks).filter( function(isSelected){return isSelected}).length
test2
const values = { featurePacks: { feature1: true, feature2: true, feature3: false, feature4: true, feature5: false, feature6: true, feature7: false, } } var tmp = _.countBy(Object.values(values?.featurePacks)).true
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
test1
test2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
test1
8441273.0 Ops/sec
test2
3247709.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and its options, pros, cons, and considerations. **Benchmark Overview** The MeasuratThat.net website allows users to create and run JavaScript microbenchmarks. The provided benchmark compares two approaches: native code (using `Object.values()` and `filter()`) and Lodash's `countBy()` function. The benchmark is defined in JSON format and consists of two test cases. **Options Compared** The benchmark compares the performance of: 1. Native code: * Using `Object.values()` to get an array of object values * Using `filter()` to filter out unwanted elements from the array 2. Lodash's `countBy()` * A function that counts the occurrences of each value in an array **Pros and Cons** **Native Code:** Pros: * Native code is typically faster since it doesn't rely on external libraries. * It can be more efficient because it avoids the overhead of a function call. Cons: * Requires manual filtering, which can lead to errors if not implemented correctly. * The code needs to handle edge cases (e.g., empty arrays, null or undefined values). **Lodash's `countBy()`:** Pros: * Simplifies the implementation by providing a pre-built function for counting occurrences. * Handles edge cases and returns an object with counts. Cons: * Requires including the Lodash library, which may not be desirable in all scenarios. * May have additional overhead due to the library and its functionality. **Other Considerations** * The benchmark uses `Object.values()` to get an array of object values. This is a modern JavaScript feature (introduced in ES6) that allows you to get an array of an object's own enumerable properties using the `.values()` method. * The Lodash function `countBy()` takes an array and returns an object with counts for each unique value. **Library Usage** The benchmark uses the Lodash library, which is a popular JavaScript utility library. It provides a wide range of functions that simplify common tasks, such as string manipulation, array operations, and more. **Special JS Features/Syntax** None are mentioned in this specific benchmark. **Alternatives** If you need to compare performance with native code and Lodash's `countBy()`, you could consider the following alternatives: 1. Using a different library, like Moment.js for date-related operations or jQuery for DOM manipulation. 2. Implementing custom functions for common tasks instead of relying on external libraries. 3. Comparing performance with other approaches, such as using a streaming algorithm or parallel processing. Keep in mind that each approach has its trade-offs and should be evaluated based on your specific use case and requirements.
Related benchmarks:
uniqBy performance
uniqBy performance ttt
uniqBy performance lodash vs native
uniqBy performance and map
lodash isnubmer vs typeof
Comments
Confirm delete:
Do you really want to delete benchmark?