I actually run some benchmarks, and the results were fascinating. Here is the scenario: I've created an array and a set with full of True values and with only one False at the end.
By the way, if you increase the number of values in an array it will take even longer because it has O(n) complexity. In essence, it looks up every item in the array one by one. Whereas set has O(1) complexity meaning it takes the same amount of time even though the number grows.
Yes, you are right. But, even if we've stored unique values inside populateArray such as alphabet(a-z) it would still have taken more time than Set. Because it still has to iterate it over with O(n) complexity. What I was trying to show is a mere demonstration of execution of speed
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I actually run some benchmarks, and the results were fascinating. Here is the scenario: I've created an array and a set with full of True values and with only one False at the end.
By the way, if you increase the number of values in an array it will take even longer because it has O(n) complexity. In essence, it looks up every item in the array one by one. Whereas set has O(1) complexity meaning it takes the same amount of time even though the number grows.
This is awesome, thank you! This should probably be its own post
is there a difference if you use Array.includes?
Not a fair comparison on several dimensions.
Set
should have only unique values. If you modifypopulateArray
to only store unique elements - that would be an improvement.Set
should typically be compared toObject
Yes, you are right. But, even if we've stored unique values inside populateArray such as alphabet(a-z) it would still have taken more time than
Set
. Because it still has to iterate it over with O(n) complexity. What I was trying to show is a mere demonstration of execution of speed