Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys without stuff
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7 };
Tests:
for-in
for (let i = 0; i < 10000; ++i) { for (const key in obj) { console.log(key); } }
Object.keys
for (let i = 0; i < 10000; ++i) { for (const key of Object.keys(obj)) { console.log(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:
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark that compares two approaches to iterate over an object's properties: `for-in` loop and using the `Object.keys()` method. **What is being tested?** In this benchmark, the following options are compared: 1. **`for-in` loop**: This is an older way of iterating over an object's properties in JavaScript. It uses a manual index (`key`) to access each property. 2. **`Object.keys()` method**: This is a built-in JavaScript method that returns an array of an object's own enumerable property names. **Pros and Cons** - **`for-in` loop**: - Pros: Can be used when the order of iteration matters or when you need to access both the key and value. - Cons: May iterate over inherited properties, which can lead to unexpected behavior. It also uses a manual index, which can be error-prone. - **`Object.keys()` method**: - Pros: More concise and readable than the `for-in` loop. Does not include inherited properties in the result. - Cons: Requires an additional import for older browsers (prior to ECMAScript 2015). May have a slight performance overhead due to the creation of an array. **Library** None, as this benchmark uses only built-in JavaScript methods and does not rely on any external libraries. **Special JS Features/Syntax** - **ECMAScript 2015+**: The `Object.keys()` method was introduced in ECMAScript 2015 (ES6). This means that older browsers may not support it. To make this benchmark compatible with older browsers, you would need to add a polyfill or use a different approach. **Benchmark Preparation Code** The provided preparation code creates an object `obj` with 7 properties and assigns them integer values from 1 to 7. **Individual Test Cases** Two test cases are defined: - **Test Case 1: `for-in` loop**: This test case uses a traditional `for-in` loop to iterate over the object's properties. - **Test Case 2: `Object.keys()` method**: This test case uses the `Object.keys()` method to retrieve an array of property names, which is then iterated using a `for...of` loop. **Latest Benchmark Result** The benchmark was run on a Chrome browser with version 89.0.4389.82 on a Linux-based desktop device. The results are: - **`for-in` loop**: Executed approximately 1.38 executions per second. - **`Object.keys()` method**: Executed approximately 0.90 executions per second. Note that the performance difference between these two approaches is relatively small, but `Object.keys()` may be slightly faster due to its optimized implementation. **Alternatives** Other alternatives for iterating over an object's properties include: - Using a `for...of` loop with the spread operator (`...`) or `Array.from()`. - Using the `for...in` loop with the `hasOwnProperty()` method to exclude inherited properties. - Using a library like Lodash, which provides a more robust and flexible way of working with objects.
Related benchmarks:
For in vs For of
for-in vs Object.keys()
Object.keys(obj)[0] vs for in
for-in vs object.keys FOR trebushnoyD
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?