Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries vs Object.keys vs for...in
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs for...in
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = {}; for (let i = 0; i < 100; i++) { obj[`k_${i}`] = i; }
Tests:
Object.entries
for (const [k, v] of Object.entries(obj)) console.log(k, v);
Object.keys
for (const k of Object.keys(obj)) console.log(k, obj[k]);
for...in
for (const k in obj) console.log(k, obj[k]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
for...in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
2174.3 Ops/sec
Object.keys
2311.8 Ops/sec
for...in
2532.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** On the provided JSON, three JavaScript microbenchmarks are defined to compare the performance of different approaches to iterate over an object: 1. `for...in` loop 2. `Object.keys()` method 3. `Object.entries()` method These methods are used to access and manipulate the properties of an object. **Options compared** The benchmark compares the performance of these three approaches: * `for...in`: a traditional loop that iterates over the properties of an object using the `in` keyword. * `Object.keys()`: returns an array of strings containing the property names of an object. * `Object.entries()`: returns an array of tuples containing the property keys and values of an object. **Pros and cons** Here's a brief summary of each approach: * **For...in**: Pros: simple and straightforward, can be used to iterate over both enumerable and non-enumerable properties. Cons: may not work correctly with some types of objects (e.g., arrays), can be slow for large objects. * **Object.keys()**: Pros: fast and efficient, only returns enumerable property names. Cons: requires an additional step to access the values associated with each key, can be slower than `for...in` in some cases. * **Object.entries()**: Pros: provides both keys and values directly, fast and efficient for large objects. Cons: only works with modern browsers that support it. **Library and special JS feature** None of these methods rely on external libraries or specific JavaScript features beyond the standard object model. However, note that `for...in` can be affected by the `Strict Mode` behavior, which was introduced in ECMAScript 5. In Strict Mode, `for...in` only iterates over enumerable properties, whereas in Non-Strict Mode, it also includes non-enumerable properties. The benchmark does not specify whether to use Strict or Non-Strict Mode. **Other alternatives** If you need to iterate over an object's properties and values, other approaches might include: * Using `forEach()` method: `obj.forEach(function(value, key) { console.log(key, value); });` * Using a `for` loop with array indices: `for (var i = 0; i < obj.length; i++) { console.log(obj[i]); }` * Using `map()`, `filter()`, and other array methods to extract the desired values. Keep in mind that these alternatives might not be as efficient or straightforward as using one of the three original approaches, depending on your specific use case.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.keys().length vs for i in object i++ vs Object.entries().length vs Object.values().length
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?