Nowadays, it’s interesting that loops become part of our life as developers and we use them at least one time a day. Because of that, one day I decided to investigate and go deeper into JavaScript loops, where I found very interesting things and if I do not share them with you, I am going to feel guilty.
Before you continue reading, I would strongly recommend you to read my previous blog which I believe you will find very useful to create a full picture of the loops. So, go on and read it.
Object properties iteration
Let’s first analyze object iteration and suppose that we have an object, something like:
var obj = {
property1: 1,
property2: 2,
…
}
First, what comes to our mind is to iterate them with the standard for each iteration:
for (var prop in obj) {
console.log(prop);
}
In this case, we are going to iterate through the object properties but is it the correct way? The answer is yes and no, depends on your needs. Another way to iterate trough is to exclude all inherited properties, which in some case we do not need. So, we can exclude them by using the JavaScript method hasOwnProperty(). You can find an explanation about in operator and hasOwnProperty() in my previous blog.
Since we learned some object optimization/improvements/usage, now the question is, can we really do an optimization?
The answer is yes. Before I am going to show you how we can do that, let’s spend some time on the loops.
Loop iteration
In order to continue the previous example, I will continue explaining the loops with object iteration (of course you can test it with a list of integers like the speed test examples or anything you want).
To accomplish that, we will need the JavaScript method Object.keys().
- Object.keys() method returns an array of a given object but only the own properties of the object
Let’s write the standard for loop:
var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
console.log(obj[keys[i]]);
}
Now we have a solution where we decreased the iteration time by eliminating the time for the evaluation `keys.length` from O(N) to O(1) which is a big time saving if we iterate big arrays.
So, during the development, if you are not limited (like applying some best practices,…), you can add another optimization, by using while loop.
var i = keys.length;
while (i--) {
console.log(obj[keys[i]]);
}
In this case, we do not declare a new variable, we don’t execute new operations and the while loop will automatically stop when it reaches -1.
Speed testing:
Since the new browsers like Chrome are very fast and optimized, in order to see the best speed differences, I would suggest executing the loops on IE where you will be able to see a real speed difference between the loops.
var arr = new Array(10000);
Example speed test 1
console.time();
for (var i = 0; i < arr.length; i++) {
// operations...
var sum = i * i;
}
Execution 1: 4.4ms
Execution 2: 5.5ms
Execution 3: 5ms
Execution 4: 4.6ms
Execution 5: 5ms
Example speed test 2
console.timeEnd();
while (i--) {
// operations...
var sum = i * i;
}
console.timeEnd();
Execution 1: 3.7ms
Execution 2: 4.8ms
Execution 3: 3.9ms
Execution 4: 3.8ms
Execution 5: 4.2ms
Thank you for reading and I would appreciate any feedback.