Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs Native reduce on object x100
(version: 0)
Comparing performance of:
Lodash vs Native
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Script Preparation code:
window.obj = {}; for (var i = 0, len = 10; i < len; i++) { var arr = [] obj['key' + i] = arr for (var n = 0, len = 10; n < len; n++) { arr.push({ ['key' + n]: n }); } }
Tests:
Lodash
_.reduce(obj, (acc, val, key) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, {})
Native
Object.entries(obj).reduce((acc, [key, val]) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
1785141.8 Ops/sec
Native
1877540.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition and test cases demonstrate the comparison of two approaches: using Lodash's `reduce` function versus the native implementation in JavaScript. **Script Preparation Code** The script preparation code creates an object `window.obj` with 100 properties, each containing an array of objects. This structure is used to test the reduction operation on the object. ```javascript for (var i = 0, len = 10; i < len; i++) { var arr = [] obj['key' + i] = arr for (var n = 0, len = 10; n < len; n++) { arr.push({ ['key' + n]: n }) } } ``` This code creates a nested object structure with 100 levels of depth, making it an ideal benchmarking case. **Html Preparation Code** The HTML preparation code includes the Lodash library version 4.17.4: ```html <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> ``` This script is used to enable the use of Lodash's `reduce` function in the benchmark. **Test Cases** There are two test cases: 1. **Lodash** ```javascript Benchmark Definition: _.reduce(obj, (acc, val, key) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, {}); ``` This implementation uses Lodash's `reduce` function to iterate over the object and transform its values. 2. **Native** ```javascript Benchmark Definition: Object.entries(obj).reduce((acc, [key, val]) => { acc[key] = val.map(d => ({ label: key, value: val })); return acc; }, {}); ``` This implementation uses the native `Object.entries` method to iterate over the object and transform its values. **Comparison** The two test cases compare the performance of Lodash's `reduce` function versus the native implementation. The benchmark measures the number of executions per second (ExecutionsPerSecond) on a Chrome 70 browser on a Windows desktop platform. **Pros and Cons** **Lodash:** Pros: * Easier to read and maintain due to its declarative syntax. * Provides a consistent API across different programming languages. Cons: * Adds overhead due to the library's existence and parsing. * May not be optimized for performance in all browsers. **Native:** Pros: * Optimized for performance, as it's a native implementation. * Does not add any additional overhead beyond JavaScript execution. Cons: * More verbose and less readable than Lodash's `reduce` function. * May require more expertise to optimize due to its native nature. **Other Considerations** When choosing between these two approaches, consider the following factors: * Readability: If readability is a top priority, use Lodash's `reduce` function for easier understanding of the code. * Performance: For high-performance requirements or in environments where every millisecond counts, choose the native implementation. * Complexity: The native implementation may require more expertise to optimize due to its native nature. **Alternatives** Other alternatives to consider when comparing performance include: * Using other libraries or frameworks that provide similar functionality (e.g., Ramda). * Implementing a custom reducer function using JavaScript's `reduce` method. * Utilizing web workers or parallel processing for concurrent execution. Keep in mind that these alternatives might not be directly comparable to the native implementation, as they may introduce additional overhead due to their own libraries and syntax.
Related benchmarks:
Lodash vs Native reduce on object - updated 2
Lodash reduce vs native
Lodash reduce vs transform vs Native reduce on object
Lodash reduce vs transform vs Native reduce on object (Object without prototype)
Comments
Confirm delete:
Do you really want to delete benchmark?