Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for (key in obj) vs Object.keys(object)
(version: 0)
Comparing performance of:
for (key in obj) vs Object.keys(obj)
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; var count = 10; do { obj["key" + count + ""] = "val" + count + ""; } while(count--); var temp = []; for(var key in obj) { temp.push(obj[key]); } var objKey = []; var keys = Object.keys(obj); for(var i = 0, len = keys.length; i < len; i++) { objKey.push(obj[keys[i]]); }
Tests:
for (key in obj)
var temp = []; for(var key in obj) { temp.push(obj[key]); }
Object.keys(obj)
var objKey = []; var keys = Object.keys(obj); for(var i = 0, len = keys.length; i < len; i++) { objKey.push(obj[keys[i]]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for (key in obj)
Object.keys(obj)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for (key in obj)
851649.8 Ops/sec
Object.keys(obj)
694870.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its results. **Benchmark Overview** The benchmark measures the performance of two approaches for iterating over an object in JavaScript: 1. Using the `for...in` loop with the `key` variable. 2. Using the `Object.keys()` method. **Script Preparation Code** The script preparation code creates a test object (`obj`) and populates it with 10 properties using a loop. The `temp` array is initialized to store the values from the object, while the `objKey` array uses `Object.keys()` to iterate over the object's keys. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only runs in the JavaScript environment and does not involve any DOM manipulation or rendering. **Library Usage** The `Object.keys()` method is a built-in JavaScript function that returns an array of strings representing the property names of an object. This function uses the ECMAScript standard, specifically section 9.3.1, to implement its behavior. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what is commonly used in modern web development. However, it's worth noting that the `for...in` loop with the `key` variable uses an older style of iteration that can be slower than more modern approaches like `Object.keys()`. **Pros and Cons of Approaches** 1. **For...in Loop with Key Variable:** * Pros: + Can be faster for very large objects, as it avoids creating an array of keys. + Some developers are familiar with this approach and may find it more intuitive. * Cons: + Can be slower due to the overhead of iterating over the object's properties using `in`. + May not work correctly if the object has non-enumerable properties or if the property names are not strings. 2. **Object.keys():** * Pros: + Generally faster and more efficient than the `for...in` loop. + Does not have issues with non-enumerable properties or non-string property names. * Cons: + May require additional memory to store the array of keys, depending on the object's size. **Other Alternatives** There are other approaches for iterating over objects in JavaScript, such as: 1. **For...of Loop:** A more modern iteration method that is gaining popularity, especially with the introduction of modern browsers' support. 2. **Array.prototype.forEach():** While not specifically designed for object iteration, `forEach()` can be used to iterate over an object's properties using a callback function. In summary, the benchmark provides a simple and straightforward comparison between two common approaches for iterating over objects in JavaScript. The results should help developers understand the relative performance characteristics of these approaches and choose the most suitable one depending on their specific use case and requirements.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
checks if object has any key - Object.keys vs for key in 2
Comments
Confirm delete:
Do you really want to delete benchmark?