Iterating through JavaScript Objects - 5 Techniques and Performance Tests

undefined or mostly null.
Search for a command to run...

undefined or mostly null.
Glad I could help :)
In this series, I will share articles related to best practices for building and deploying applications on the web with the JavaScript and Python programming languages.
As earlier stated in my previous article ES6 modules is a very powerful concept. Although support is not available everywhere yet, a common way of using it is to transpile into ES5. You can use Grunt, Gulp, Webpack, Babel or some other transpiler to ...
I wrote this article in December 2023 for an interview screening task and thought to share it today while clearing my drafts for some new content prep, just in case it's still helpful to someone out t

Hey! I’m super excited to announce that I have joined the Secretariat Team at the Digital Public Goods Alliance. In this role, I leverage my passion for open source and technical background to assess DPG applications, provide technical support and ad...

Creative designs have become more important than ever in the software ecosystem today with many industries and end-consumers having several use cases that require them to offer design editing solutions to either designers or end-consumers. Every busi...

With the rise of artificial intelligence (AI) and large language models (LLMs), it has become easier to solve different human problems than ever before. Even consumers with little to no technical expertise can benefit from AI. Humans can now automate...

Some years ago, GitHub introduced the new Profile README feature that allowed GitHub users to pin a markdown file on their profile using a special repository named after their GitHub username. Since then, developers have used this file as a quick por...

Developers tend to know how to iterate through JavaScript Arrays easily but most times they tend to get confused while working with JavaScript Objects especially beginners and intermediates. In this article, I'll show you Five (5) different ways of iterating through JavaScript Objects and some performance comparison tests to show you which is faster and more efficient.
Object properties, besides a value, have three special attributes (also known as “flags”):
true, can be edited, else it's read-only.true, then listed in loops.true, the property can be deleted and these attributes can
be modified.When we create a property “the usual way”, all of them are true. But we can
change them anytime.
The method Object.getOwnPropertyDescriptor allows us to query the full information about a property.
let user = {
name: "Bolaji"
};
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
console.log(descriptor);
// {value: "Bolaji", writable: true, enumerable: true, configurable: true}
Enumerable properties are those properties whose internal enumerable flag is set to true, which is the default for properties created via simple assignment.
Basically, if you create an object via obj = {foo: 'bar'} or something
thereabouts, all the properties are enumerable.
The for...in loop statement can be used to iterate over all non-Symbol, enumerable properties of an object.
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
// key1 value1
// key2 value2
// key3 value3
The Object.keys() method returns an array of Object keys. This creates an array that contains the properties of the object. You can then loop through the array to get the keys and values you need.
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
let items = Object.keys(obj);
console.log(items);
// ["key1", "key2", "key3"]
items.map(key => {
let value = obj[key];
console.log(key, value)
});
// key1 value1
// key2 value2
// key3 value3
The Object.values() method returns an array of Objects Values. This creates an array that contains the properties of the object. You can then loop through the array to get the keys and values you need.
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
let items = Object.values(obj);
console.log(items);
// ["value1", "value2", "value3"]
items.map(value => {
console.log(value)
});
// value1
// value2
// value3
The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object. This creates an array that contains the properties of the object. You can then loop through the array to get the keys and values you need.
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
let items = Object.getOwnPropertyNames(obj);
console.log(items);
// ["key1", "key2", "key3"]
items.map(key => {
let value = obj[key];
console.log(key, value)
});
// key1 value1
// key2 value2
// key3 value3
The Object.entries() method returns an array of a given object's own enumerable property [key, value] pairs.
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
let items = Object.entries(obj);
console.log(items);
// 0: ["key1", "value1"]
// 1: ["key2", "value2"]
// 2: ["key3", "value3"]
items.map(item => {
let key = item[0];
let value = item[1];
console.log(key, value);
});
// key1 value1
// key2 value2
// key3 value3
Now let's test all these techniques and compare each one based on their speed and performance to determine which is faster and much efficient
Most browsers like Chrome and Firefox implement high-resolution timing in performance.now(). The performance.now() method returns a DOMHighResTimeStamp, measured in milliseconds.
Usage
let start = performance.now();
// code to be timed...
let duration = performance.now() - start;
Let’s begin testing…
According to our tests, here are the results in ascending order;

So, according to these results, the fastest way to iterate through JavaScript Objects is the for…in loop. Now, this doesn't mean the other methods are void or useless, it all depends on use cases.
The problem with a for...in loop is that it iterates through properties in the Prototype chain. It iterates over object properties. Javascript arrays are just a specific kind of object with some handy properties that help you treat them as arrays, but they still have internal object properties and you don't mean to iterate over these. for...in also iterates over all enumerable properties and not just the array’s elements. This can also lead to unexpected results.
When you loop through an object with the for...in loop, you need to check if the property belongs to the object. You can do this with hasOwnProperty.
A better and more efficient way to loop through objects in ES6 is to first convert the object into an array using Object.keys() , Object.values() , Object.getOwnPropertyNames or Object.entries() . Then you loop through the array to get the keys and values.