In terms of time complexity (and language agnostic), what algorithm will be most efficient.
let array = [ { a : 1, b : 2}, {a : 3, b : 6}, {a : 4, b : -1} .... N ]
Now imagine a function, find({a : 3}) i.e. find the position of the object in the array where property a is equals to 3. What algorithm (pseudo code) will be the fastest here?
probably if you don't use binary search-trees you will end up with a worstcase of O(n) and a best case of O(1).
you could build indexes with all "a"s and so on to get an 0(1) in read time complexity -> like '{"a-1" : 0, "a-3" : 1, "a-4" : 2 }' but you increase space complexity in favor of time complexity.
if you need a more complex system where a or b maybe doesn't exist you could think of bitmasks as a prefilter to have a reduced set for the real filtering a = "01011101001" as an "bit representation of the position of all sets with "a"s
or you could just use [].filter() and keep it simple for starters :)
the next question is do you need always just 1 result ? .reduce() would be 1 answer to that.
you could use the workers to sort your hashmaps on regular basis to optimize them but than you have the classic CAPs problem :) "when to trust my query" :) you could solve this with a central list queue "deterministic state machines"
lol. I literally googled the same thing earlier today. Mine was a little more complicated though, I had to search through nested objects/arrays. I guess the most efficient way depends on the language (if for loops are faster than a while loop etc.) but unless the array size is large (thousands of entries) then I'd say it doesn't make too much difference.
If you can order either A or B then you could section the array up or something which would make looping the numbers of entries smaller.
Peter Scheler
JS enthusiast
If you want one Item:
array.find(object => object.a === 3) // returns { a: 3, b: 6 }If you want all Items:
array.filter(object => object.a === 3) // returns [{ a: 3, b: 6 }, ... ]This is ES2016. The build in functions are optimised and relative fast.