Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash for-in vs native for-in (lodash version: 4.17.10)
(version: 3)
lodash for-in vs native for-in (lodash version: 4.17.10)
Comparing performance of:
Native for-in vs lodash for-in vs Native for-in 2 vs lodash for-in 2
Created:
7 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.10/lodash.min.js'></script>
Script Preparation code:
var obj = Array.from({ length: 100000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {})
Tests:
Native for-in
for (const prop in obj) { if (obj.hasOwnProperty(prop)) { const v = obj[prop]; } }
lodash for-in
_.forIn(obj, function(prop){if (obj.hasOwnProperty(prop)) { const v = obj[prop]; } })
Native for-in 2
for (const prop in obj) { if (obj.hasOwnProperty(prop)) { const v = obj[prop]; } }
lodash for-in 2
_.forIn(obj, function(prop){if (obj.hasOwnProperty(prop)) { const v = obj[prop]; } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Native for-in
lodash for-in
Native for-in 2
lodash for-in 2
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark created using MeasureThat.net. The benchmark compares the performance of two approaches: native `for...in` loops and the `_.forIn` method from the Lodash library. **Native `for...in` Loops** The first three test cases use native `for...in` loops to iterate over an object's properties. A native `for...in` loop is a built-in JavaScript feature that allows iterating over an object's enumerable properties using a loop. Pros: * Native implementation, which means it's optimized for performance by the JavaScript engine. * Easy to understand and implement. Cons: * May have slower performance due to various overheads like garbage collection, type checking, etc. * Can be affected by issues like property ordering or prototype chain traversal. **Lodash `_.forIn` Method** The last two test cases use the Lodash library's `_.forIn` method to iterate over an object's properties. This method is a utility function that abstracts away the underlying native implementation, providing a more convenient and predictable way of iterating over objects. Pros: * Provides a standardized and predictable way of iterating over objects, which can lead to better performance due to optimized internal logic. * Abstracts away the complexities of native `for...in` loops, making it easier to write and maintain code. Cons: * Requires an additional library (Lodash) to be included in the project, which may add overhead or increase bundle size. * May have slightly slower performance compared to native implementation due to the abstraction layer. **Other Considerations** * The benchmark uses a specific object creation strategy (`Array.from` and `reduce`) to generate a large dataset for testing. This approach can help ensure consistent results and avoid issues related to property ordering or prototype chain traversal. * The benchmark is executed on Chrome 119, which may not represent the entire JavaScript landscape (e.g., older browsers or Edge). Future benchmarks should aim to cover more browsers and platforms to ensure broader relevance. **Alternatives** If you need to compare performance of other iteration methods or approaches in your code, consider exploring these alternatives: * `forEach`: A built-in method for iterating over arrays using a callback function. * `for...of` loops: A newer iteration mechanism introduced in ECMAScript 2015 (ES6) that allows iterating over iterable objects like arrays and sets. * `Promise.all()`: A method for executing multiple promises concurrently, which can be used to test performance of asynchronous iteration methods. * Custom iteration mechanisms using techniques like iterators or async generators.
Related benchmarks:
lodash vs for-of vs forEach
for-of Object.entries vs for-in
lodash vs for-of vs forEach es6
lodash foreach vs for-of vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?