Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.keys + for loop
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.keys + for loop
Created:
2 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
for (let i=10000; i > 0; i--) { for (const key in obj) { console.log(key); } }
Object.keys
for (let i=10000; i > 0; i--) { Object.keys(obj).forEach(key => console.log(key)); }
Object.keys + for loop
for (let i=10000; i > 0; i--) { const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { console.log(keys[i]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.keys + for loop
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):
Let's break down the provided benchmark definition and individual test cases to understand what is being tested. **Benchmark Definition** The benchmark definition is a JSON object that defines a JavaScript microbenchmark. It consists of two main parts: 1. **Script Preparation Code**: This code sets up the environment for the benchmark. In this case, it creates an object `obj` with 7 properties and assigns them all equal to 1. 2. **Html Preparation Code**: This is empty in the provided example, but it's likely used to prepare the HTML environment for the benchmark. **Individual Test Cases** There are three test cases: 1. **for-in** 2. **Object.keys** 3. **Object.keys + for loop** Each test case has a unique JavaScript code that performs a similar task: iterating over the properties of the `obj` object and logging their values to the console. **What is being tested?** The benchmark is testing three different approaches to iterate over an object's properties: 1. **for-in**: This method uses the `in` operator to iterate over the object's own enumerable properties. 2. **Object.keys**: This method returns an array of the object's own enumerable property names. 3. **Object.keys + for loop**: This approach first gets an array of property names using `Object.keys`, and then iterates over that array using a traditional `for` loop. **Options compared** The benchmark is comparing the performance of these three approaches: * **Pros and Cons:** + **for-in**: - Pros: Simple, intuitive, and efficient for small objects. - Cons: Can be slower than other methods for large objects, as it returns an array of all property names, not just the ones that are actually used. + **Object.keys**: - Pros: Fast, efficient, and modern approach. Returns only the property names that are actually used. - Cons: Requires JavaScript 5 or later to work. + **Object.keys + for loop**: - Pros: Flexible, allows for arbitrary filtering of properties. - Cons: More complex, requires an extra step to get the array of property names. * **Other considerations:** + The benchmark is running on a Chrome 112 browser on a Mac OS X 10.15.7 device, which may affect the results due to browser-specific optimizations and platform limitations. **Library/Functionality** None of the test cases use any external libraries or functions beyond the built-in `Object.keys` method. **Special JS feature/syntax** The only notable aspect is that the benchmark uses JavaScript 5 or later, as required by the `Object.keys` method.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
for-in vs for object.keys keys
for-in vs object keys map vs object keys loop
Comments
Confirm delete:
Do you really want to delete benchmark?