Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys map
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
for-in
const arr = []; for (var i=10000; i > 0; i--) { for (var key in obj) { arr.push(key); } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(obj).map(key => key); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1.2 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 17 on iOS 17.1.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
2369.2 Ops/sec
Object.keys
951.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Goal:** The goal of this benchmark is to compare the performance of three different approaches for iterating over an object and extracting its keys: 1. Using `for...in` loop 2. Using `Object.keys()` method 3. Using `map()` function with `Object.keys()` **Options Compared:** * **For-in Loop**: Iterates over an object using the `for...in` loop, which iterates over the object's properties (including inherited ones) and assigns them to a variable in each iteration. * **Object.keys() Method**: Returns an array of strings containing the property names of an object. * **Map Function with Object.keys()**: Uses the `map()` function to apply a transformation to each key in the object, which returns an array of keys. **Pros and Cons:** * **For-in Loop**: + Pros: Simple and intuitive, can be used for other purposes besides iterating over objects (e.g., iterating over arrays). + Cons: Iterates over inherited properties, can lead to unexpected behavior if not handled carefully. * **Object.keys() Method**: + Pros: Fast and efficient, returns only the property names of the object, does not iterate over inherited properties. + Cons: May not be suitable for all use cases (e.g., when using older browsers that don't support it). * **Map Function with Object.keys()**: + Pros: Can be used to perform additional transformations on each key, can be more memory-efficient than creating an array of keys. + Cons: Can be slower due to the extra overhead of `map()` function. **Library Used (if any):** None in this case. The benchmark is testing only JavaScript core features and standard libraries. **Special JS Feature or Syntax:** There's no specific special feature or syntax being tested here, just standard JavaScript core features. Now, let's talk about the benchmark preparation code: * **Script Preparation Code**: Creates an object `obj` with 11 properties (keys) and assigns a value of 1 to each property. * **Html Preparation Code**: None in this case. The benchmark is testing only JavaScript performance, so no HTML or CSS is involved. **Other Alternatives:** Some other alternatives for iterating over objects and extracting keys include: * Using `for...of` loop (which iterates over the object's own properties) * Using a `forEach()` function with an arrow function * Using a library like Lodash or Ramda, which provide more functional programming style APIs for working with data structures. Keep in mind that these alternatives may have different performance characteristics and use cases than the options being tested here.
Related benchmarks:
for-in vs object.keys v3
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
for-in vs object keys map vs object keys loop
for-in vs object.keys vs object.values for objects - output object values
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?