Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries vs for-in loop
(version: 0)
Comparing performance of:
object entries vs for in
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 1, b: 2, c: 3, d: 4, 0: 'a', 1: 'b' };
Tests:
object entries
for(const [key, value] of Object.entries(obj)){ console.log(key, value); }
for in
for(const key in obj){ console.log(key, obj[key]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object entries
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object entries
26573.2 Ops/sec
for in
29997.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark definition consists of two parts: `Script Preparation Code` and `Html Preparation Code`. The `Script Preparation Code` defines a JavaScript object called `obj` with multiple properties. This object will be used as input for both test cases. The `Html Preparation Code` is empty, which means that no HTML-related code needs to be executed before running the benchmark. **Options Compared** Two options are compared in this benchmark: 1. **`for-in` loop**: This is a traditional way of iterating over an object's properties using the `in` keyword. 2. **`Object.entries()` method**: Introduced in ECMAScript 2015 (ES6), `Object.entries()` returns an array of a given object's key-value pairs, allowing for more flexible and expressive iteration. **Pros and Cons** * **`for-in` loop**: + Pros: Simple, widely supported, and familiar to many developers. + Cons: Iterates over the object's properties in no particular order (i.e., `0`, `1`, etc. are used as property names), which can lead to unexpected behavior or performance issues if not handled carefully. * **`Object.entries()` method**: + Pros: More efficient and concise way of iterating over an object's key-value pairs, with predictable results. + Cons: Introduced in ES6, so older browsers may not support it. Additionally, some developers might find the syntax unfamiliar. **Library Used** There is no library used in this benchmark. The `Object.entries()` method is a native JavaScript function. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's standard in ES6. **Other Alternatives** If you need to compare iteration over objects, here are some alternative approaches: 1. **`for...in` loop with `hasOwnProperty()`**: You can add a check using the `hasOwnProperty()` method to ensure that only own properties of the object are iterated. ```javascript for (var key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]); } } ``` 2. **`Object.keys()`, `Array.prototype.forEach()`, and `Map`**: You can use `Object.keys()` to get an array of an object's own enumerable property names, then iterate using `Array.prototype.forEach()`. Alternatively, you can create a `Map` from the object and iterate over its entries. ```javascript for (var key of Object.keys(obj)) { console.log(key, obj[key]); } // or var map = new Map(Object.entries(obj)); map.forEach((value, key) => console.log(key, value)); ``` These alternatives might offer different performance characteristics depending on the specific use case and browser support. Keep in mind that these alternatives are more complex than the original `for-in` loop and `Object.entries()` method used in this benchmark.
Related benchmarks:
obj vs array
For in vs For of
Performance of Object.values(obj) vs_.values() vs for-in to extract values from an object
Object.entries vs Object.keys vs for...in
Object entries vs forin
Comments
Confirm delete:
Do you really want to delete benchmark?