Which is faster for Loop or .hasOwnProperty?

I worked on a project where I needed to list excluded users from a giant list of user data. This made me wonder if it is faster to use double for loop with excluded id in array . Or, if you put the identifier in the properties of the object and use .hasOwnProperty() 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 method:

 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 understand that there are other ways to make loops faster, for example, to store lengths in variables. I tried to simplify this.

Thanks for any info.

+4
source share
4 answers

If you think about it, it would be prudent that the .hasOwnProperty() approach would be faster because it uses only 1 for loop .

WRONG

I was a little surprised. I expected a double loop to be slower. But I think you cannot rate the for loop speed.

Double cycle

Although it would seem to me the slowest, in fact it was the fastest bench at 7,291,083 ops/sec

.hasOwnProperty ()

I see how it will be slower, because functions are slower than operators. These benches are at 1,730,588 ops/sec

if..in

@Geuis's answer included the if..in statement and thought that I would check the speed, which would seem to be the fastest, but with a bench at 2,715,091 ops/sec it still does not beat the for loops.

Conclusion

For loops fast. Double loops are faster 4 times faster than using .hasOwnProperty() and almost 3 times faster than when using the if..in condition. However, performance is not very noticeable; so speed is really important, you need to complicate the situation. In my opinion, the if..in method is the way to go.

Check it out yourself in your browser. I used Google Chrome 28 .

Update

It’s important to note that using the for..in ad will give you better performance.

+2
source

You missed the third, faster alternative. If you did not execute Object.prototype any way, and the identifier is unlikely to be a prototype (for example, valueOf , etc.), you can simply use the for loop like this:

 for(var i=0; i < mainList.length; i++) { if (eObject[mainList[i].id] !== undefined) {//or typeof eObject[mainList[i].id] !== 'undefined' //do something } } 

Note the updated JSPref , this is the fastest way (57 252 850 op / sec versus 17 503 538 op / sec for double loop)

+6
source

Change to show the correct code for future internet traders: visit http://jsperf.com/stackoverflow-for-vs-hasownproperty/5 for comparison

 var testVal = 'BigBrownFox', arr = [1,4,'asd','BigBrownFox',9]; if( arr.indexOf('testVal') > -1 ){ //do something } 

To test an array of values ​​to exist 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 is slightly disabled in both cases.

If you are using an array, use the indexOf function. If a test value exists, it will return its index. Otherwise, it is -1 if not found. No loop is needed at all.

In the case of an object, you are not using .hasOwnProperty. Yes, it does what you need, but its harder and slower because you are making 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.

0
source

in chrome the fastest cycle for

in older / other browsers, the fastest while loop is.

especially if you cache the length (very important if mainList is big)

as I see you only have lines in eObject, I also suggest using (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 } } 
0
source

Source: https://habr.com/ru/post/1491376/


All Articles