JavaScript performance: simple key search in object vs value search in array -


i need emulate set in javascript — i.e. variable able answer question "do contain x?".

performance of insertion/deletion doesn't matter. order doesn't matter. , isn't multiset.

there 2 ways implement it:

  1. using regular array value search:

    var set = [17, 22, 34]; if (set.indexof(x)!=-1) ...; 
    • 1a. using typedarray (e.g. int32array), when possible:

      var set = int32array.of(17, 22, 34); if (set.indexof(x)!=-1) ...; 
  2. using object key search:

    var set = {17: true, 22: true, 34: true}; if (set[x]) ...; 

theoretically object key search should faster (depending on how implemented in js engine, should either o(log(n)), or o(1) — vs o(n) on array value search). however, case in javascript (where access object member may require multiple lookups) — on small sets dozens of items? assuming values in set quite simple (either integers, or short strings).

resume. want know: minimum amount of set items required make object key search faster array value search in case of: (1) values integers; (2) values short strings — in modern (2015/2016) web-browsers?

i understand can perform measurements myself, seems irrational make every developer measure same things — put question here in case have done it.

i curious , have done of these analysis using same stack jsperm on nodejs 6.3:

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

the input data array of various sizes ( 2^x x = 0 .. 11 ) , short keys of 1 2 chars.

tests broken insertion, lookup, , insertion + lookup.

results are:

  • lookup using object ( hashmap ) faster 16 elements up
  • insert super slow object keys
  • insert + lookup, arrays faster regardless of size

duplicates in dataset have considered well...


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -