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 for..in vs Object.keys
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, e: 7, f: 3, g: 14, h: 1, i: 88, j: 0, k: 5, 0: 'a', 1: 'b', str: 'long string', nested: { nested: 0 }, hello: 55.5 };
Tests:
Object.entries
for (const [key, val] of Object.entries(obj)){ console.log(key, val); }
for..in
for (let key in obj) { console.log(key, obj[key]) }
Object.keys
for (let key of Object.keys(obj)) { console.log(key, obj[key]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
for..in
Object.keys
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/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
14994.3 Ops/sec
for..in
16361.9 Ops/sec
Object.keys
14347.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on MeasureThat.net: **Benchmark Definition** The website is testing three different approaches to iterate over an object in JavaScript: 1. **`for..in`**: This is a traditional method of iterating over an object's properties using the `for...in` loop. 2. **`Object.entries()`**: This is a modern method introduced in ECMAScript 2015 (ES6) that returns an array of a given object's key-value pairs, allowing for iteration using a simple `for...of` loop. 3. **`Object.keys()`**: This method returns an array of an object's own enumerable property names. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`for..in`**: + Pros: Wide support across older browsers, easy to understand for those familiar with traditional JavaScript. + Cons: May not work as expected if an object has inherited properties from its prototype. Also, it returns both own enumerable and non-enumerable properties. * **`Object.entries()`**: + Pros: Modern method, works with modern browsers, and provides a clean and efficient way to iterate over key-value pairs. + Cons: May not work in older browsers (pre-ES6), can be slower due to array operations. * **`Object.keys()`**: + Pros: Fast and efficient, works well in most modern browsers. + Cons: Returns only enumerable properties, may not work if an object has non-enumerable properties. **Other Considerations** When choosing between these approaches, consider the following: * If you need to support older browsers or require a more traditional iteration method, use `for..in`. * For modern browsers and efficient key-value pair iteration, use `Object.entries()`. However, be aware that some older browsers may not support this method. * When working with objects that have many properties, using `Object.keys()` might be faster due to the optimized lookup. **Library/Functionality** In the benchmark, we see that three functions are used: 1. **`Object.entries()`**: Returns an array of key-value pairs for a given object. Introduced in ECMAScript 2015 (ES6). 2. **`for...in`**: Iterates over an object's properties using the `for...in` loop. 3. **`Object.keys()`**: Returns an array of an object's own enumerable property names. **Special JS Features/Syntax** The benchmark uses modern JavaScript features, such as: * **Arrow functions**: Used in the script preparation code (`var obj = {...} => { ... }`). * **Template literals**: Used in the HTML preparation code (e.g., `"\r\n\t0: 'a',\r\n \t1: 'b',\r\n"`). However, there's no specific mention of special JavaScript features or syntax that are not present in the benchmark. **Alternatives** If you need to test other iteration methods or want to explore alternative approaches, consider: * **`for...of`**: Similar to `Object.entries()`, but with a more traditional `for...loop` structure. * **Using `Array.prototype.forEach()`**: Iterates over an array of values using the `forEach()` method. Keep in mind that MeasureThat.net already tests these alternatives, so you can refer to their results for more information.
Related benchmarks:
For in vs For of
Object.keys vs Object.values
Object.entries vs Object.keys vs for...in
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?