JavaScript · Middle · Question 69 of 75
Why convert a large list of allowed IDs to a Set before many lookups?
- ✓Correct answer:
set.has(x)is about constant time, whilearray.includes(x)scans the whole array each time - 2Sets use less memory than arrays
- 3Arrays can't contain numbers as IDs
Explanation
For one lookup it doesn't matter; for thousands inside a loop, O(n) per check turns into O(n²) overall.