Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.forIn vs for in
(version: 0)
Comparing performance of:
_.forIn vs for in
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:
var obj = {}; for(i=0; i<10000; i++){ var number = (i + 1); obj['key-' + number] = number }
Tests:
_.forIn
var array1 = []; _.forIn(obj, function (val, key) { array1.push(val) });
for in
var array2 = []; var key; for (key in obj) { if (obj.hasOwnProperty(key)) { array2.push(obj[key]) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.forIn
for in
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 break down the provided benchmark and its options. **Benchmark Overview** The benchmark compares two JavaScript iteration approaches: `_.forIn` from Lodash library and traditional `for...in`. The test creates an object with 10,000 properties (keys) and iterates over it using both methods, pushing values to an array for comparison. **Options Compared** 1. **_.forIn**: This is a function from the Lodash library that iterates over objects using a custom callback function (`val` and `key` parameters). It's designed to work with objects in a more functional programming style. 2. **Traditional `for...in`**: A built-in JavaScript loop that iterates over object properties, including inherited ones. **Pros and Cons** ### _.forIn Pros: * More efficient: Lodash's implementation is optimized for performance. * Less verbose: The callback function reduces boilerplate code. * Works with any iterable (not limited to objects). Cons: * External dependency: Requires the Lodash library. * Not part of standard JavaScript: May not be supported by older browsers or environments. ### Traditional `for...in` Pros: * No external dependencies needed. * Part of standard JavaScript, making it more widely compatible. * Easy to understand and implement. Cons: * More verbose due to the need for a separate check (`obj.hasOwnProperty(key)`). * Inefficient: Iterates over inherited properties, which may not be desirable in all cases. **Library** In this benchmark, Lodash is used as the library. Its primary purpose is to provide utility functions for functional programming and object manipulation. In this specific case, `_.forIn` is used to simplify the iteration process. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. It only uses standard JavaScript constructs. **Alternatives** If you wanted to implement a similar benchmark using only standard JavaScript, you could use a traditional `for...in` loop without checking for inherited properties: ```javascript var array = []; for (var key in obj) { if (obj.hasOwnProperty(key)) { array.push(obj[key]); } } ``` However, this approach is less efficient and more verbose than the Lodash implementation. Keep in mind that this benchmark focuses on comparing two specific iteration approaches rather than exploring other optimization techniques or edge cases.
Related benchmarks:
lodash vs for-of vs forEach5453
lodash vs nativejs foreach
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
Loop over object: lodash.forOwn vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?