Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getOwnPropertyNames vs Object.keys - performance
(version: 0)
Comparing performance of:
getOwnPropertyNames vs Object.keys
Created:
3 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.getOwnPropertyNames(obj); let n = 0; for (let i = 0; i < arr.length; i++) { const k = arr[i]; if (k.charAt(0) == '$') continue; n++; }
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++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getOwnPropertyNames
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getOwnPropertyNames
30381512.0 Ops/sec
Object.keys
30031426.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Purpose** The benchmark compares the performance of two approaches to get an array of property names from an object: `Object.keys()` and the `getOwnPropertyNames` method in combination with a manual filtering process. **Options Compared** Two options are compared: 1. **`Object.keys()`**: This method returns an array of a given object's own enumerable property names. 2. **`getOwnPropertyNames` + manual filtering**: The `getOwnPropertyNames` method returns an array of all property names, including non-enumerable ones. To filter out properties whose names start with `$`, the benchmark uses a manual loop to iterate over the array and skip those properties. **Pros and Cons** * **`Object.keys()`**: + Pros: Simple, efficient, and widely supported. + Cons: Excludes non-enumerable property names, which may be important in certain scenarios. * **`getOwnPropertyNames` + manual filtering**: + Pros: Includes all property names, including non-enumerable ones. The filtering process can be customized to suit specific use cases. + Cons: More complex and computationally intensive due to the manual loop. **Library** The `Object.keys()` method is a built-in JavaScript method that is part of the ECMAScript standard. It's widely supported across different browsers and environments. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's considered "standard" by the ECMAScript standard. However, it's worth noting that `getOwnPropertyNames` is a legacy method from older versions of JavaScript (pre-ES6), whereas `Object.keys()` is a modern method introduced in ES5. **Other Alternatives** If you need to get an array of property names and don't mind excluding non-enumerable ones, other alternatives might include: * Using the `for...in` loop with a custom filtering process. * Utilizing a library like Lodash or Underscore.js that provides utility functions for working with objects. * Converting the object to an array using `Object.values()` and then filtering it. Keep in mind that these alternatives may have different performance characteristics, trade-offs, or requirements depending on your specific use case.
Related benchmarks:
Variable assignment from object | traditional vs destructuring
Delete vs destructure for objects
Delete vs destructure for cloned objects
Delete vs destructure vs reduce for objects
Delete vs destructure for objects v2 2
Comments
Confirm delete:
Do you really want to delete benchmark?