Object key versus array search performance

Example 1:

["member1", "member2",...,..., "member100000"]

Example 2:

{
    "member1": true, // (doesn't really need values, only keys :/)
    "member2": true,
    "...",
    "member100000": true
}

I store the elements in an array on each piece of content, as in Example 1, but by doing this, I would have to iterate through 49999 elements in my array to find the 50000 element, so I just thought to check that a specific key is defined in the object javascript would be the best approach here, although I don't need to store the value, but just check if the key is undefined or not?

I need to check for example. "member50000" exists as a value inside my array - or as a key inside my object.

I did some benchmarking tests, but I'm not sure if I came to the right conclusion, or if I am doing something wrong in comparison: http://jsperf.com/lolda123

, / , (true), if(obj["member50000"]) ? ? , , , , , , , .

, - , , , , , ?

+4
2

, , Object , !

, , jsPerf, NodeJS 6.3 ( V8):

https://github.com/amoldavsky/js-array-indexof-vs-hashmap

:

  • ( , HashMap) ! ,
  • , array.indexOf

, , , . array.indexOf .

+1

obj.hasOwnProperty , arr.indexOf http://jsperf.com/array-hasownproperty-vs-array-indexof

arr.indexOf , , .

0

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


All Articles