Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getOwnPropertyNames vs loop vs cached property names vs entries
(version: 0)
Comparing performance of:
getOwnPropertyNames vs Cached property names vs for in vs Object.keys vs Object.entries
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { $names : ['a','b','c'], a:1, b:2, c:3 }
Tests:
getOwnPropertyNames
var ks = Object.getOwnPropertyNames(obj); var n = 0; for (var i = 0; i < ks.length; i++) { var k = ks[i]; if (k.charAt(0) == '$') continue; n++; }
Cached property names
var ks = obj['$names']; var n = 0; for (var i = 0; i < ks.length; i++) { var k = obj[ks[i]]; n++; }
for in
var n = 0; for (var k in obj) { if (k.charAt(0) == '$') continue; n++; }
Object.keys
var ks = Object.keys(obj); var n = 0; for (var i = 0; i < ks.length; i++) { var k = ks[i]; if (k.charAt(0) == '$') continue; n++; }
Object.entries
var n = 0; for (const [k, v] of Object.entries(obj)){ if (k.charAt(0) == '$') continue; n++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
getOwnPropertyNames
Cached property names
for in
Object.keys
Object.entries
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):
To explain what is being tested on the provided JSON, let's break it down: The benchmark measures the performance of four different approaches to iterate over an object's properties: 1. `getOwnPropertyNames` (native method) 2. Using cached property names (`obj['$names']`) 3. Using a traditional for-in loop (`for (var k in obj)`) 4. Using `Object.keys()` (native method) These approaches are compared to determine which one is the fastest. Let's analyze each approach: **1. getOwnPropertyNames** This is a native JavaScript method that returns an array of property names that can be accessed using dot notation (e.g., `obj.a`). This approach is often used when you need to iterate over an object's properties and perform some operation on them, but you don't care about the property values. Pros: * Fast and efficient * Native implementation, which means it's optimized for performance Cons: * Only works with objects that have a `$names` property (in this case, `obj.$names = ['a', 'b', 'c']`) **2. Using cached property names** This approach uses the `$names` property to cache the property names and then iterates over them using an array index. Pros: * Fast, since it avoids the overhead of creating an object with property names * Works with any object that has a `$names` property Cons: * Assumes that the `$names` property is available, which might not always be the case * May have some overhead due to the caching mechanism **3. for-in loop** This traditional approach uses a for-in loop to iterate over an object's properties. Pros: * Widely supported and easy to implement * Works with any object that supports the for-in loop syntax Cons: * Can be slower than native methods like `getOwnPropertyNames` or `Object.keys()`, since it involves more overhead due to the loop mechanics * May not handle certain edge cases, such as inherited properties or non-enumerable properties **4. Object.keys()** This is another native JavaScript method that returns an array of property names for an object. Pros: * Fast and efficient, similar to `getOwnPropertyNames` * Works with any object that has a `$names` property (in this case, `obj.$names = ['a', 'b', 'c']`) Cons: * Only works with objects that have a `$names` property Now, let's consider the test results: The benchmark shows that the fastest approach is using cached property names (`obj['$names']`). This might be because it avoids the overhead of creating an object with property names and uses the existing `$names` array. In comparison, `getOwnPropertyNames` is slightly slower than `Object.keys()`, while the for-in loop approach is slower due to its additional overhead. The test results can be useful for developers who want to optimize their code performance when working with objects that have a large number of properties.
Related benchmarks:
undefined vs. hasOwnProperty
Object.keys vs Object.getOwnPropertyNames - objects with 0 keys
hasOwnProperty string vs number
computed property names vs create, assign
in vs hasOwn
Comments
Confirm delete:
Do you really want to delete benchmark?