I was working on a project where I needed to pull a list of excluded users out of a giant list of user data. It made me wonder if it is faster to use a double for loop
with excluded id's in an array
. Or if putting the id's in object properties and using .hasOwnProperty()
is faster.
var mainList = LARGE JSON OBJECT OF DATA.
var eArray = ["123456","234567","345678","456789","012345"];
var eObject = {"123456":"0","234567":"0","345678":"0","456789":"0","012345":"0"};
Using the Double For Loop
Approach:
for(i=0; i < mainList.length; i++){
for(j=0; j < eArray.length; j++){
if(mainList[i]['id'] === eArray[j]){
//Do Something
}
}
}
Using the .hasOwnProperty()
Approach:
for(i=0; i < mainList.length; i++){
if(eObject.hasOwnProperty(mainList[i]['id'])){
//Do Something
}
}
I realize there are other ways to make the loops faster, like storing lengths in variables. I've tried to simplify this.
Thanks for any information.
The last built-in method we will discuss for objects is hasOwnProperty. This method has an O(1) or constant time and works by returning true or false depending on if the key is present in the object.
Object. keys looks up all own, enumerable properties (oh, and arrays are fast).
The hasOwnProperty() method in JavaScript is used to check whether the object has the specified property as its own property. This is useful for checking if the object has inherited the property rather than being it's own.
For inherited properties, in will return true . hasOwnProperty , as the name implies, will check if a property is owned by itself, and ignores the inherited properties.
If you think about it, it would make sense that the .hasOwnProperty()
approach would be faster because it uses only 1 for loop
.
I was actually a little surprised. I was expecting the double loop to be slower. But I guess you can't under estimate the speed of a for loop
.
While this to me would seem like the slowest, this actually ended up being the fastest benching at 7,291,083 ops/sec
I can see how this would be slower because functions are slower than statements. This benches at 1,730,588 ops/sec
@Geuis answer included the if..in statement and thought I would test the speed of that which would seem the fastest but benching at 2,715,091 ops/sec
it still doesn't beat the for loops.
For loops are fast. The double loops runs more than 4 times faster than using .hasOwnProperty()
and almost 3 times faster than using the if..in
condition. However, the performance is not really noticeable; so is speed really that important that you have the need to complicate things. In my opinion the if..in
method is the way to go.
Test this yourself in your browser. I was using Google Chrome 28
.
Update
It is important to note that using an for..in
declaration will give you the best performance.
You've missed out on a third, faster alternative. Provided you haven't been tinkering with the Object.prototype
in any way, and the ID's are unlikely to be prototype values (like valueOf
and the like), you could simply use a for
loop like so:
for(var i=0; i < mainList.length; i++)
{
if (eObject[mainList[i].id] !== undefined)
{//or typeof eObject[mainList[i].id] !== 'undefined'
//do something
}
}
Check the updated JSPref, it's the fastest way by far (57,252,850 ops/sec vs 17,503,538 ops/sec for the double loop)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With