Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getOwnPropertyNames() vs Object.keys() vs for ... in
(version: 0)
Comparing performance of:
Object.keys() vs getOwnPropertyNames() vs for ... in loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { $props : ['a','b','c'], a:1, b:2, c:3 }
Tests:
Object.keys()
const arr = Object.keys(obj); let n = 0; for (let i = 0; i < arr.length; i++) { const k = arr[i]; if (k.charAt(0) == '$') continue; n++; }
getOwnPropertyNames()
const arr = Object.getOwnPropertyNames(obj); let n = 0; for (let i = 0; i < arr.length; i++) { const k = arr[i]; if (k.charAt(0) == '$') continue; n++; }
for ... in loop
let n = 0; for (const k in obj) { if (k.charAt(0) == '$') continue; n++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.keys()
getOwnPropertyNames()
for ... in loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys()
9906305.0 Ops/sec
getOwnPropertyNames()
9410627.0 Ops/sec
for ... in loop
26696090.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares three methods for extracting property names from a JavaScript object: * **`Object.keys(obj)`:** This method returns an array containing the names of all enumerable properties on an object. It's a common and straightforward way to get property names. * **Pros:** Easy to understand and use, widely supported. * **Cons:** Doesn't include inherited properties (those from the prototype chain), can be slower than `getOwnPropertyNames()` for large objects. * **`Object.getOwnPropertyNames(obj)`:** This method returns an array containing the names of all own properties on an object, including non-enumerable ones. * **Pros:** More comprehensive as it includes inherited properties. Generally faster than `Object.keys()`. * **Cons:** More complex syntax than `Object.keys()`. * **`for ... in loop`:** This classic iteration method loops through the enumerable properties of an object and allows direct access to their names. * **Pros:** Fine-grained control over the iteration process, can be optimized for specific use cases. * **Cons:** Can be less efficient than `Object.keys()` or `getOwnPropertyNames()`, more verbose syntax. **Considerations:** * **Performance:** The benchmark results show that `for ... in` performs best in this particular case (on a Mac with Chrome 119). However, performance can vary significantly depending on factors like the size of the object, the browser engine, and the specific implementation details. * **Accuracy:** Do you need to include inherited properties? If not, `Object.keys()` might suffice. If you need all own properties, even non-enumerable ones, then `getOwnPropertyNames()` is preferable. **Alternatives:** There are a few other ways to extract property names from JavaScript objects: * **Symbol Properties:** These properties can be accessed using `Object.getOwnPropertySymbols(obj)`. This method returns an array of symbols representing the symbol properties of an object. Let me know if you have any more questions!
Related benchmarks:
Get props
Get props
Object.keys vs for in vs Object.getOwnPropertyNames
Object.keys vs for in vs Object.getOwnPropertyNames with a twist
Object.keys().includes() vs hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?