Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys (own props)
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
4 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 }; var o1, lo1, io1, vo1, ko1, ko1s;
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { console.log(key); }} }
Object.keys
for (var i=10000; i > 0; i--) { o1 = obj; ko1s = Object.keys(o1); lo1 = ko1s.length; for(io1=0; io1<lo1; io1++) { ko1 = ko1s[io1]; vo1 = o1[ko1]; console.log(ko1) } }
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for iterating over an object's properties: `for-in` loop and `Object.keys()` method with a loop. **Script Preparation Code** The script preparation code sets up an object `obj` with 7 properties, all initialized with the value 1. This object will be used as input for both benchmarking scenarios. **Benchmark Test Cases** There are two individual test cases: 1. **"for-in"`**: This test case uses a traditional `for-in` loop to iterate over the object's properties. The loop iterates from 10,000 down to 0, and for each iteration, it prints out the current property value using `console.log(key)`. 2. **"Object.keys"`**: This test case uses the `Object.keys()` method to get an array of the object's property names, and then loops over this array using a traditional `for` loop. For each iteration, it accesses the corresponding property on the original object using the bracket notation (`o1[ko1]`) and prints out its value using `console.log(ko1)`. **Library Used** In both test cases, no external libraries are used beyond the built-in JavaScript `Object` and `Array` objects. However, it's worth noting that some browsers may have additional libraries or features that might affect the benchmark results. **Special JS Features/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax. It relies on standard JavaScript constructs like loops, objects, and arrays. **Comparison of Approaches** The two approaches differ in how they iterate over the object's properties: * `for-in` loop: Iterates directly over the object's property names using a proprietary syntax (`var key in obj`). This approach allows for direct access to the property name, but can be slower due to its proprietary nature. * `Object.keys()` method with a loop: Uses the `Object.keys()` method to get an array of property names and then loops over this array. This approach is generally faster and more efficient than the `for-in` loop, as it leverages the optimized implementation of `Object.keys()`. **Pros and Cons** * **For-in**: Pros: + Direct access to property name + Can be useful in certain scenarios where property names are important * Cons: + Proprietary syntax + May be slower due to its proprietary nature * **Object.keys() method with a loop**: + Generally faster and more efficient than `for-in` + Leverages optimized implementation of `Object.keys()` **Other Alternatives** If you're looking for alternative approaches, you could consider using other methods like: * Using the `forEach()` method on an array of property names * Using a library like Lodash or Ramda to iterate over objects However, these alternatives may not be as efficient or straightforward as the original `Object.keys()` approach. I hope this explanation helps!
Related benchmarks:
For in vs For of
Object.keys vs for in vs Object.getOwnPropertyNames with a twist
Loop over object: lodash vs Object.entries vs Object.keys vs Object.values vs for of vs for in vs keys for of
for in vs for of --
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?