Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.forIn vs. _.each on Object.keys vs. for...in (v2)
(version: 0)
Comparing performance of:
_.forIn vs _.each(Object.keys) vs for...in
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
https://cdn.jsdelivr.net/combine/npm/lodash@4.17.21,npm/lodash@4.17.21/forIn.min.js
Tests:
_.forIn
const vehicle = { color: 'blue', mileage: 100000, year: 2014 } const logKey = key => console.log(key) _.forIn(vehicle, logKey)
_.each(Object.keys)
const vehicle = { color: 'blue', mileage: 100000, year: 2014 } const logKey = key => console.log(key) _.each(Object.keys(vehicle), logKey)
for...in
const vehicle = { color: 'blue', mileage: 100000, year: 2014 } const logKey = key => console.log(key) for (const key in vehicle) { logKey(key) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.forIn
_.each(Object.keys)
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):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, compares the performance of three approaches for iterating over object keys in JavaScript: Lodash's `_.forIn`, `_.each(Object.keys)`, and traditional `for...in`. The goal is to determine which approach is the fastest. **Options Compared** 1. **Lodash's _.forIn**: This function iterates over an object using a specific syntax, which is similar to `for...in` but with more features for handling object keys. 2. **Lodash's _.each(Object.keys)**: This function uses the `Object.keys()` method to get an array of object keys and then calls `_each` on that array. 3. **Traditional for...in**: This is a standard JavaScript syntax for iterating over object properties using a loop. **Pros and Cons** 1. **Lodash's _.forIn**: * Pros: More features than `for...in`, such as automatic handling of non-enumerable properties, and more concise syntax. * Cons: May have additional overhead due to the use of Lodash's internal implementation. 2. **Lodash's _.each(Object.keys)**: * Pros: Uses a standard JavaScript method (`Object.keys()`) and can be easily chained with other Lodash functions. * Cons: Creates an array of keys, which may incur additional memory allocation and copying overhead. 3. **Traditional for...in**: * Pros: Simple and well-established syntax, no additional dependencies required. * Cons: Can be slower due to the use of a loop and potential issues with handling non-enumerable properties. **Library and Purpose** Lodash is a popular JavaScript utility library that provides many functional programming helpers. In this case, it's used for iteration over object keys. **Special JS Feature or Syntax** None mentioned in the provided benchmark definition. **Other Alternatives** If you need to iterate over object keys, other approaches could be: * Using `Object.entries()` and a loop to access both key-value pairs. * Using `for...of` loops with an array of keys (e.g., from `Object.keys()`) if you're targeting modern browsers. * Writing a custom iteration function using the `in` operator or `hasOwnProperty()` method. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash.each vs ES6 for
lodash.each vs ES6 FOR vs Native - v.2
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
Lodash forOwn vs Native keys + forEach 1000 keys
Comments
Confirm delete:
Do you really want to delete benchmark?