Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For in vs For object.keys() vs lodash each
(version: 0)
Comparing performance of:
For in vs For object keys vs Lodash each
Created:
2 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 obj = {a:1, b:2, c:3, d:4, f:5, h:6, j:7, i:8, k:9, o:0};
Tests:
For in
var result = {}; for(let i in obj) { result[i] = obj[i]; }
For object keys
var result = {}; var keys = Object.keys(obj); for(let i=0; i<obj.length; ++i) { result[i] = obj[i]; }
Lodash each
var result = {}; _.each(obj, (v, i) => result[i] = v);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For in
For object keys
Lodash each
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For in
9431660.0 Ops/sec
For object keys
68245360.0 Ops/sec
Lodash each
4851720.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark you provided. **Benchmark Definition** The benchmark defines three test cases that compare different approaches to iterate over an object: 1. **For in**: uses the `for...in` loop to iterate over the object's properties. 2. **For object keys**: uses the `Object.keys()` method to get an array of the object's property names, and then iterates over it using a traditional `for` loop. 3. **Lodash each**: uses the `_each()` function from the Lodash library to iterate over the object's properties. **Options Compared** The benchmark compares three different approaches to achieve the same result: * `For in`: uses the built-in `for...in` loop, which iterates over both enumerable and non-enumerable properties. * `For object keys`: uses the `Object.keys()` method to get an array of only enumerable property names, and then iterates over it using a traditional `for` loop. * `Lodash each`: uses a third-party library (Lodash) to iterate over the object's properties. **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: * **For in**: + Pros: simple, fast, and widely supported. It iterates over both enumerable and non-enumerable properties. + Cons: may include non-enumerable properties in the iteration, which can be undesirable depending on the use case. * **For object keys**: + Pros: only enumerates over enumerable property names, which can be more predictable than `for...in`. + Cons: requires using an array method (`Object.keys()`), which may incur a small overhead. It also assumes that all properties are enumerable. * **Lodash each**: + Pros: provides a simple and efficient way to iterate over objects, especially for those already familiar with Lodash's functionality. + Cons: introduces an external dependency (Lodash) and may incur a small performance overhead due to the function call. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for working with arrays, objects, and other data structures. The `_each()` function used in this benchmark allows you to iterate over an array or object without having to write explicit loop logic. In this case, it's used to iterate over the `obj` object's properties. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark beyond what's required for the iteration logic. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **For...of**: This is a more modern iteration approach that can be used with objects. However, it's only supported in ECMAScript 2015+ and may not work in older browsers. * **Array.prototype.forEach()**: Another way to iterate over an array or object using a callback function. Overall, the choice of iteration approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Performance of Object.values(obj) vs_.values() vs for-in to extract values from an object
lodash vs for-of vs forEach5453
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?