Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.js vs Javascript (meanBy)
(version: 1)
Comparing performance of:
Native vs Lodash
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
const data = Array.from({ length: 1_000_000 }, (_, i) => ({ value: i % 100 })); function nativeMeanBy(array, key) { const sum = array.reduce((acc, obj) => acc + obj[key], 0); return sum / array.length; } function lodashMeanBy(array, key) { return _.meanBy(array, key); }
Tests:
Native
nativeMeanBy(data, 'value')
Lodash
lodashMeanBy(data, 'value')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
116.8 Ops/sec
Lodash
138.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON compares the performance of two methods for calculating the mean of a set of objects: a native JavaScript implementation and one using the Lodash library. Here’s a breakdown of the components, their pros and cons, and other relevant considerations. ### Comparison Overview: **Options Compared:** 1. **Native Method (`nativeMeanBy`)** - **Implementation:** Uses JavaScript's built-in `Array.prototype.reduce` method to sum values and compute the mean. - **Function Code:** ```javascript function nativeMeanBy(array, key) { const sum = array.reduce((acc, obj) => acc + obj[key], 0); return sum / array.length; } ``` 2. **Lodash Method (`lodashMeanBy`)** - **Implementation:** Utilizes the Lodash library's `_.meanBy` function to perform the same operation. - **Function Code:** ```javascript function lodashMeanBy(array, key) { return _.meanBy(array, key); } ``` ### Performance Results: - **Lodash:** - Executions per second: 138.29 - **Native JavaScript:** - Executions per second: 116.76 ### Pros and Cons: #### Native Method: **Pros:** - **No external dependencies:** This method only relies on standard JavaScript, making it lighter and ensuring it works in any environment where JavaScript is supported. - **Control and clarity:** Developers have full control over the implementation, which can be tailored or optimized further if necessary. **Cons:** - **Potentially less optimized:** While JavaScript engines are highly optimized, the native implementation might not leverage certain optimizations that a library like Lodash can provide. #### Lodash Method: **Pros:** - **Conciseness and readability:** Using Lodash can make code shorter and potentially easier to read, especially for developers familiar with its syntax. - **Optimization in handling various cases:** Lodash functions are meticulously optimized; they may perform better or handle edge cases more gracefully than a custom implementation. **Cons:** - **Dependency overhead:** Including the Lodash library can increase the size of the payload for a web application, which might be a concern for performance and loading times. - **Additional complexity:** Using libraries introduces another layer of abstraction and might complicate debugging. ### Other Considerations: - **Special JS Features:** The benchmark does not utilize any special JavaScript features beyond standard array manipulation methods. - **Alternatives to Lodash:** There are several alternatives to Lodash for performing similar operations, including: - **Underscore.js:** An earlier library with many of the same utilities as Lodash. - **Native JavaScript Solutions:** As demonstrated in the benchmark, using built-in array methods like `map`, `reduce`, and `filter`. - **Other libraries**: Such as Ramda or Immutable.js, which offer functional programming features tailored for various use cases. ### Conclusion: This benchmark exemplifies the trade-offs between using a library versus native JavaScript. While Lodash offers convenience and potentially better optimization, the native method is lightweight and can be customized without introducing external dependencies. Developers should consider factors like package size, performance, readability, and maintainability when choosing between these approaches.
Related benchmarks:
Lodash sumBy vs reduce 100 elements
Average js vs ._mean
lodash vs manual
[2024] Lodash .sumBy vs. Reduce with / 1000
[2024] Lodash .sumBy vs. Reduce with / 10002
Lodash meanBy vs JS (with small and large size)
Lodash meanBy vs js vs reduce lodash
Lodash.js vs Javascript (meanBy) 2
Lodash.js vs Javascript (meanBy) 3
Comments
Confirm delete:
Do you really want to delete benchmark?