Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Improving the intersection method

Suppose we have the following two sets:

  • setA with values [1, 2, 3, 4, 5, 6, 7]
  • setB with values [4, 6]

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} ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content