When Use Arrays, Tuples, Maps, and Sets In Typescript with Examples

danywalls

Dany Paredes

Posted on August 14, 2023

When Use Arrays, Tuples, Maps, and Sets In Typescript with Examples

A few days ago, a friend asked how to prevent duplicate keys in an array, and I told him there are other collections to work with, and each one is suited for a specific situation. Because I love the NBA, I tried to find an easy way to explain each data structure using the context of basketball.

In the basketball is all about strategy, and using the right players at the right time can make all the difference. So the choice of data structures—arrays, tuples, maps, and sets—can greatly influence the game. Let's dive into each one to understand when to use them, when not to use them, or their disadvantages.

Let's start:

Arrays

Arrays are like a team roster, containing a list of players. It can change over time, adding or removing players as needed.

Read the more about Arrays

Example:

A list of players drafted for the upcoming season.

let teamRoster: string[] = ["LeBron James", "Stephen Curry", "Kevin Durant"];
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Dynamically sized.

  • Players (elements) can be accessed directly using their position (index).

Disadvantages:

  • Lacks strict type checks across positions.

When to Use:

  • When you have a list of similar items, like all point guards or all rookies.

When to Avoid:

  • Fixed-size collections or varied data types.

Tuples

Tuples are like specifying exact players for specific positions; each position is predefined.

Learn more about Tuples

Example: A fixed starting lineup, where positions and players are predefined.

let startingLineup: [string, string, string, string, string] = ["Point Guard", "Shooting Guard", "Small Forward", "Power Forward", "Center"];
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Strict structure for fixed-size data.

  • Each position (index) has a specific type.

Disadvantages:

  • Not flexible; can't easily change the lineup size.

When to Use:

  • When positions and players are fixed, such as a specific game's starting five.

When to Avoid:

  • Variable roster sizes or changing lineups.

Maps

Maps are like a team’s jersey numbers. Each player (value) has a unique jersey number (key).

Learn more about Maps

Example: Associating each player with their jersey number.

let jerseyNumbers = new Map<string, number>();
jerseyNumbers.set("LeBron James", 6);
jerseyNumbers.set("Stephen Curry", 30);
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • We can have any key and Value

  • Keeps the insertion order.

Disadvantages:

  • It might be overkill for simple key-value associations.

When to Use:

  • When associating players with stats, awards, or any specific data.

When to Avoid:

  • Simple lists or when an object might suffice.

Sets

Sets are like All-Star game rosters. No player is picked twice; everyone is unique.

Learn more about Set

Example: A set of players selected for the All-Star game.

let allStars = new Set<string>();
allStars.add("LeBron James");
allStars.add("LeBron James");  // He's already an All-Star!
Enter fullscreen mode Exit fullscreen mode

Advantages:

  • Ensures uniqueness.

  • Quick membership checks.

Disadvantages:

  • No key-value associations.

When to Use:

  • Unique collections like Hall of Fame inductees or MVP winners.

When to Avoid:

  • When duplicates are acceptable or key-value pairings are needed.

Conclusion

In conclusion, TypeScript provides a variety of data structures, each with pros and cons. Our job is to identify the strengths and potential of each one. I hope these examples make it easy to know when to choose or avoid a structure and help in making decisions and selecting the most appropriate data structure for each specific situation.

💖 💪 🙅 🚩
danywalls
Dany Paredes

Posted on August 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related