How to check if key exists in map (for a map with many items) #1626
-
I would like to check if a cue value exists as a key in the above map, and this code works perfectly However since this is imperative, and we have still get all keys and loop through the keys it is not ideal performance wise with 0(n) complexity. It takes very long with 200++ items in the map. Is there a better way to validate if key exists in map without a for a loop with O(1) time complexity? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
What may cause a performance issue is that you are generating a big disjunction with A possibility would be to define another field which does some kind of assertion: items: {
"item1": 1
"item2": 2
}
item: string
itemExists: true & items[item] != _|_
item: "item1"
// item: "item3" // will fail |
Beta Was this translation helpful? Give feedback.
What may cause a performance issue is that you are generating a big disjunction with
or
, more than thefor
comprehension itself.A possibility would be to define another field which does some kind of assertion:
https://cuelang.org/play/?id=SI8GGu0F3WI#cue@export@cue