Reputation: 1572
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.
Upvotes: 10
Views: 10544
Reputation: 6052
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.
Upvotes: 7
Reputation: 42277
Edit to show the proper code for future internet traversers: Visit http://jsperf.com/stackoverflow-for-vs-hasownproperty/5 for the comparison
var testVal = 'BigBrownFox',
arr = [1,4,'asd','BigBrownFox',9];
if( arr.indexOf('testVal') > -1 ){
//do something
}
For testing an array of values for existence in another array:
var testVal = ['BigBrownFox'],
arr = [1,4,'asd','BigBrownFox',9];
for(var i=0, len=testVal.length; i<len; i++){
if( arr.indexOf(testVal[i]) > -1 ){
//do something
}
}
Actually, your approach in both cases is slightly off.
If are using an array, just use the indexOf
function. If the testing value exists, it will return its index. Otherwise it's -1 if not found. No loop is needed at all.
In the case of an object, you don't use .hasOwnProperty. Yeah, it does what you want but its overcomplicated and slower because you're doing a function call.
Just use
var eObject = {"123456":"0","234567":"0","345678":"0","456789":"0","012345":"0"};
if( '234567' in eObject ){ //do something }
Hope this helps.
Upvotes: 1
Reputation: 16706
in chrome the fastest loop is for
in older/other browsers the fastest one is the while-- loop.
especially if you cache the length.(very important if the mainList is big)
as i see you have only strings in eObject i also suggest to use (eObject[mainList[i].id])
which is faster than (eObject[mainList[i].id] !== undefined)
var i=mainList.length;
while(i--){
if (eObject[mainList[i].id]) {
//do something
}
}
Upvotes: 0
Reputation: 76395
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)
Upvotes: 8