April 2018
Beginner to intermediate
426 pages
10h 19m
English
Suppose we have the following two sets:
Using the intersection method we created, we would need to iterate the values of setA seven times, which is the number of elements in setA, and compare these seven values with only two elements from setB. It would be better if we had the same result if we only had to iterate setB two times. Fewer iterations means a cheaper processing cost, so let's optimize our code in order to iterate the set with fewer elements, as follows:
intersection(otherSet) { const intersectionSet = new Set(); // {1} const values = this.values(); // {2} const otherValues = otherSet.values(); // {3} let biggerSet = values; // {4} ...