DEV Community

Discussion on: Learn JavaScript Sets (Simple Yet Powerful Built-In Objects)

Collapse
 
ogzhanolguncu profile image
Oğuzhan Olguncu • Edited

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.

Collapse
 
nas5w profile image
Nick Scialli (he/him)

This is awesome, thank you! This should probably be its own post

Collapse
 
kenn9123 profile image
Kenneth

is there a difference if you use Array.includes?

Collapse
 
ilya_sher_prog profile image
Ilya Sher • Edited

Not a fair comparison on several dimensions.

  • Set should have only unique values. If you modify populateArray to only store unique elements - that would be an improvement.
  • Set should typically be compared to Object
Collapse
 
ogzhanolguncu profile image
Oğuzhan Olguncu

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