Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterate through a large object
(version: 0)
Note that this doesn't work with non-enumerable properties.
Comparing performance of:
for in vs keys vs getOwnPropertyNames
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.obj = {}; for (let i = 0; i < 10000; i++) { obj[`p${i}`] = Math.random(); } delete obj.p0;
Tests:
for in
for (const key in obj) { window.result = obj[key]; }
keys
const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { window.result = obj[keys[i]]; }
getOwnPropertyNames
const keys = Object.getOwnPropertyNames(obj); for (let i = 0; i < keys.length; i++) { window.result = obj[keys[i]]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for in
keys
getOwnPropertyNames
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, specifically designed to measure the performance of three different approaches for iterating through an object: `for in`, `Object.keys()`, and `Object.getOwnPropertyNames()`. **What is tested?** In this benchmark, 3 test cases are compared: 1. **Iterating through an object using `for in`**: This approach iterates over all enumerable properties of the object. 2. **Iterating through an object using `Object.keys()`**: This approach uses the `keys()` method to get an array of all own property names (enabling us to use a for loop). 3. **Iterating through an object using `Object.getOwnPropertyNames()`**: This approach uses the `getOwnPropertyNames()` method, which returns an array containing all property names that are directly accessible on the object. **Options compared** The three test cases aim to measure how efficiently each approach can iterate over the object's properties and access their values. **Pros and Cons of each approach:** 1. **`for in`**: This is a legacy way of iterating over objects, as it also iterates over non-enumerable properties (e.g., those inherited from the prototype chain). It's generally slower than using `Object.keys()` or `getOwnPropertyNames()`, which are more efficient. 2. **`Object.keys()` and `Object.getOwnPropertyNames()`**: These two methods provide a safe way to iterate over object properties without accessing non-enumerable ones. The choice between them depends on whether you want to include inherited properties (using `Object.keys()`) or not (using `getOwnPropertyNames()`). 3. **Cons of `for in`:** Iterates over both enumerable and non-enumerable properties, which can lead to unexpected behavior if you don't control the prototype chain. 4. **Pros of `Object.keys()` and `getOwnPropertyNames():`** - Faster than `for in`. - Provide a safe way to iterate over object properties. - Can be used with loops (like for). 5. **Cons of `Object.keys()` and `getOwnPropertyNames()`:** - May not include inherited properties if using `getOwnPropertyNames()`, which could lead to missing some values. - Could have performance overhead due to extra operations performed by the method. **Special JS feature or syntax** In this benchmark, no special JavaScript features or syntax are used beyond standard ECMAScript. The only notable syntax is the use of template literals (`\r\n obj[`p${i}`] = Math.random();`) in the script preparation code.
Related benchmarks:
Object.assign vs inner for
Object.keys.includes vs object.hasOwnProperty
For in vs Object.entries 2
For in vs Object.entries Check
Comments
Confirm delete:
Do you really want to delete benchmark?