Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getOwnPropertyNames vs Object.keys vs for ... in
(version: 0)
yeah
Comparing performance of:
keys vs getOwnPropertyNames vs for ... in
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:
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
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
keys
getOwnPropertyNames
for ... in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
keys
32769372.0 Ops/sec
getOwnPropertyNames
38497428.0 Ops/sec
for ... in
61259712.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, compares the performance of three approaches to iterate over the properties of an object: 1. `for...in` 2. `Object.getOwnPropertyNames()` 3. `Object.keys()` (with a custom filter) **Approach 1: `for...in`** The `for...in` loop iterates over the object's property names, including inherited ones. This approach is not recommended for performance-critical code because it can be slower due to the following reasons: * It returns all properties, including non-enumerable ones (e.g., methods, event handlers). * The iteration order may vary across browsers and environments. * Some engines might use a slower algorithm for property iteration. **Pros:** * Simple and concise syntax. * No additional dependencies required. **Cons:** * Potential performance impact due to the inclusion of non-enumerable properties. * Unpredictable iteration order. **Approach 2: `Object.getOwnPropertyNames()`** This method returns an array containing all property names, including non-enumerable ones. The use of this approach is generally discouraged in modern JavaScript development due to its potential performance impact and less predictable behavior compared to other methods: * Returns all properties, including non-enumerable ones. * May be slower than other approaches. **Pros:** * Unambiguous result set (all property names). * No filter needed for the loop. **Cons:** * Potential performance impact due to the inclusion of non-enumerable properties. * Less predictable iteration order compared to `for...in`. **Approach 3: `Object.keys()` with custom filter** This approach uses `Object.keys()` to get an array of property names and then applies a custom filter to exclude properties starting with `$`. The use of this approach provides better performance due to the optimized filtering: * Returns only enumerable property names. * Provides control over which properties are included in the iteration. **Pros:** * Better performance compared to `Object.getOwnPropertyNames()`. * Easy to implement custom filtering logic. **Cons:** * Requires additional code for filter implementation. * Might require browser support for newer JavaScript features (e.g., arrow functions). **Library Usage** In this benchmark, the `obj` object is defined using a template string, which includes special characters like `$`. The use of these characters in the benchmark suggests that MeasureThat.net supports running JavaScript tests with custom properties. The library used here is not explicitly mentioned, but it's likely that the test uses a subset of the ECMAScript standard. **Special JS Feature** The benchmark uses `template literals` (introduced in ECMAScript 2015) to define the `obj` object. This feature allows for more readable and efficient string concatenation. The use of template literals is generally considered best practice in modern JavaScript development. **Other Alternatives** If you need to compare these approaches, you could consider using a different testing framework or benchmarking tool, such as: * Benchmark.js * js-perf * Benchmark These tools provide more advanced features and customization options for writing and running benchmarks.
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?