Javascript: which search is faster: array.indexOf versus object hash?

I need to do a lot of searches parsing xmlStream if I need some kind of tag or not.

I can do this using the array.indexOf method (I have about ~ 15 elements in the array) or by searching for the [key] object.

The second solution seems to be more effective in theory for me, but does not look beautiful in my code. But if it is really more effective, I would leave it as it is.

eg:.

var tags = [
    'tag1',
    'tag2',
    'tag3',
    ...
];

var tags2 = {
    'tag1' : null,
    'tag2' : null,
    'tag3' : null,
}

tags.indexOf(value) // exists?
tags2[value] // exists?
+13
source share
2 answers

, . indexOf O (n), hash - O (1), 15 , , .

+6

Array.indexOf() Object , Array.indexOf(). jsperf.

:

10000 : 26 547 /
100000 : 2493 /

10000 : 152 115 /
100000 : 150 450 /

+17

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


All Articles