Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getOwnPropertyNames vs Object.keys
(version: 0)
yeah
Comparing performance of:
getOwnPropertyNames vs for ... in
Created:
6 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:
getOwnPropertyNames
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++; }
for ... in
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 (2)
Previous results
Fork
Test case name
Result
getOwnPropertyNames
for ... in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getOwnPropertyNames
46521324.0 Ops/sec
for ... in
62834376.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **What is being tested?** The benchmark is comparing two approaches to iterate over an object's property names: 1. `getOwnPropertyNames` (also known as the "object property iterator" in ECMAScript): This method returns a static view of an object's own enumerable property names. 2. The `for ... in` loop: This syntax allows iterating over an object's properties, including those that are not enumerable. **Options compared** The two approaches being tested are: 1. **getOwnPropertyNames**: Using the `Object.keys()` method to get an array of property names and then iterating over it. 2. **for ... in**: Using a traditional `for` loop with the `in` keyword to iterate over the object's properties. **Pros and cons** **getOwnPropertyNames**: Pros: * More efficient, as it avoids the overhead of checking each property's enumerable status. * Can be faster for large objects. Cons: * Requires an additional method call (`Object.keys()`), which might incur additional overhead. * May not work correctly in older browsers that do not support `Object.keys()`. * Returns only own enumerable properties (not inherited ones). **for ... in**: Pros: * Simple and widely supported, as it's a built-in syntax. * Works correctly with all objects, including inherited ones. Cons: * Slower due to the overhead of checking each property's enumerable status. * Can be slower for large objects, as it has to iterate over all properties. **Library/Functionality Used** None of the test cases use any external libraries or functions besides the built-in `Object.keys()` method and the JavaScript `for` loop syntax. **Special JS Features/Syntax** The only special feature used in these test cases is the `in` keyword in the `for ... in` loop, which is a part of the traditional `for` loop syntax. This keyword allows iterating over an object's properties. **Alternatives** Other alternatives for iterating over an object's property names could be: * Using the `Object.getOwnPropertyNames()` method (which returns only own enumerable and non-writable properties). * Using a library like Lodash or Underscore.js, which provide utility functions for working with objects. * Using a custom implementation, such as using a recursive function to iterate over an object's properties. However, these alternatives are not tested in this benchmark, and the results may vary depending on the specific use case and performance requirements.
Related benchmarks:
Object property vs array on small array
Delete vs destructure for objects
Delete vs destructure for cloned objects
Delete vs destructure for objects v2 2
Explode vs assignation
Comments
Confirm delete:
Do you really want to delete benchmark?