Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.chain then reduce vs Object.entries then reduce
(version: 0)
Comparing performance of:
Lodash reduce vs Native reduce
Created:
5 years ago
by:
Registered User
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 users = {} for (var i = 0; i < 1000; i++) { if (i % 2) { users[i] = {a: "a", b: "b"} } else { users[i] = {b: "b"} } }
Tests:
Lodash reduce
var res = _.chain(users) .reduce((acc, a, userID) => { if (a === "a") { acc.push(userID) } return acc }, []) .value()
Native reduce
var res = Object.entries(users) .reduce((acc, val) => { if (val[1].a) { acc.push(val[0]) } return acc }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash reduce
Native reduce
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):
Let's dive into explaining the benchmark and its options. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case, which compares two approaches for reducing an object in a specific scenario: using Lodash's `chain` method with `reduce`, and using native JavaScript's `Object.entries` method with `reduce`. **Script Preparation Code** The script preparation code generates a dataset of 1000 objects, where every other object has a property `a` set to `"a"`. The purpose of this data is to create a scenario that triggers the reduction logic in the benchmark. **Html Preparation Code** The HTML preparation code includes a reference to Lodash's JavaScript library (`lodash.min.js`) via a CDN. This ensures that Lodash is available for use in the benchmark. **Individual Test Cases** There are two test cases: 1. **Lodash reduce**: This test case uses Lodash's `chain` method with `reduce`. The benchmark definition code creates an array of user IDs using this approach. 2. **Native reduce**: This test case uses native JavaScript's `Object.entries` method with `reduce`. The benchmark definition code creates an array of user IDs using this approach. **Comparison** The two approaches are compared in terms of performance (expressions per second). Lodash's `chain` method and `reduce` is expected to outperform the native JavaScript approach, as it is a more optimized implementation of the reduction logic. **Pros and Cons** **Lodash reduce:** Pros: * Optimized implementation of reduction logic * Potential for better performance Cons: * Additional overhead due to the use of Lodash library * May introduce additional memory usage **Native reduce:** Pros: * No additional overhead or memory usage * Native JavaScript implementation Cons: * May be slower than optimized Lodash implementation * More prone to errors and bugs due to manual implementation **Other Considerations** The benchmark is likely using the `Benchmark.js` library, which provides a standardized framework for creating benchmarks. This ensures that the results are consistent across different environments and browsers. **Library and Special JS Features** In this case, Lodash is used as a third-party library, providing a convenient and optimized implementation of the reduction logic. There are no special JavaScript features or syntax mentioned in the benchmark definition code. **Alternatives** Other alternatives to Lodash's `chain` method with `reduce` could include: * Using other libraries like Underscore.js or Ramda * Implementing the reduction logic manually using native JavaScript * Using a different data structure, such as an array of objects instead of an object with user IDs. However, these alternatives are not currently being tested in this benchmark.
Related benchmarks:
Loop over object: lodash vs Object.entries
Loop over object: lodash vs Object.entries fork by d9k 2
Loop over object: lodash vs Object.entries fork by d9k 3
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
Comments
Confirm delete:
Do you really want to delete benchmark?