Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys ForEach vs for-in hasOwnProperty
(version: 0)
Original source: https://jsperf.com/object-keys-foreach-vs-for-in-hasownproperty
Comparing performance of:
A vs B vs C vs D
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
// Declaring our Animal object var Animal = function () { this.name = 'unknown'; this.getName = function () { return this.name; } return this; }; // Declaring our Dog object var Dog = function () { // A private variable here var private = 42; // overriding the name this.name = "Bello"; // Implementing ".bark()" this.bark = function () { return 'MEOW'; } return this; }; // Dog extends animal Dog.prototype = new Animal(); // Creating an instance of Dog. var obj = new Dog(); function iterateA() { var ret = ""; for (var prop in obj) { if( obj.hasOwnProperty( prop ) ) { ret += obj[prop]; } } return ret; } function iterateB() { var ret = ""; Object.keys(obj).forEach(function (prop) { ret += obj[prop]; }); return ret; } function iterateC() { var ret = ""; for (var i = 0, keys = Object.keys(obj); i < keys.length; i++) { ret += obj[i]; } return ret; } function iterateD() { var ret = ""; for (var keys = Object.keys(obj), len = keys.length, i = 0; i < len; i++) { ret += obj[i]; } return ret; }
Tests:
A
var tmp = iterateA();
B
var tmp = iterateB();
C
var tmp = iterateC();
D
var tmp = iterateD();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
A
B
C
D
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
A
2406405.2 Ops/sec
B
3221296.2 Ops/sec
C
4528368.5 Ops/sec
D
4542309.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of different approaches to iterate over an object's properties in JavaScript. The test cases use two objects, `Animal` and `Dog`, which are created using the `function` keyword. **Object.keys() vs for-in hasOwnProperty** The benchmark compares the performance of three methods: 1. **for-in hasOwnProperty**: This method iterates over the object's own enumerable properties (using `hasOwnProperty()` to filter out inherited properties). 2. **Object.keys().forEach()**: This method uses the `Object.keys()` function to get an array of the object's property names, and then iterates over that array using `forEach()`. 3. **Manual iteration using for loops**: Two additional methods are used: `iterateC()` and `iterateD()`, which use traditional `for` loops with `Object.keys()` and indexing into the array, respectively. **Options Comparison** Here's a brief summary of each option: * **For-in hasOwnProperty**: This method is generally considered the most efficient way to iterate over an object's own properties. However, it can be slower than using `Object.keys()` if the object has many inherited properties. * **Object.keys().forEach()**: This method is convenient and easy to read, but it creates a new array of property names, which can lead to higher memory usage. Additionally, `forEach()` can be slower than traditional `for` loops due to its overhead. * **Manual iteration using for loops**: + `iterateC()`: Uses indexing into the `Object.keys()` array, which can lead to slower performance due to the need to calculate the array length and bounds. + `iterateD()`: Similar to `iterateC()`, but uses a more concise syntax by calculating the array length and loop bounds directly. **Pros and Cons** * **For-in hasOwnProperty**: Pros: efficient, easy to read. Cons: can be slower if object has many inherited properties. * **Object.keys().forEach()**: Pros: convenient, easy to read. Cons: creates new array of property names, potentially slower due to overhead. * **Manual iteration using for loops**: Pros: control over loop bounds and indexing, cons: can be slower due to indexing calculations. **Library Usage** None of the test cases explicitly use a library or external dependency. **Special JS Features/Syntax** The benchmark uses JavaScript features like `function` declarations, object creation, property names, and array iteration methods. However, these are standard JavaScript features and do not require special knowledge or expertise to understand. **Alternatives** If you were to rewrite this benchmark, you might consider additional alternatives, such as: * Using a different data structure, like an array or a Map. * Introducing more complex logic or side effects within the iteration loop. * Using async/await or callbacks for asynchronous iterations. * Using web workers or other parallel execution techniques.
Related benchmarks:
For in vs For of
Object.keys vs _Object.keys vs Object_keys
Object.entries vs Object.keys vs for...in
for-in hasOwnProperty vs object.keys for-of test vs for-in
for in Object.keys vs foreach Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?