1
00:00:02,310 --> 00:00:09,270
So as you can tell, maps in a way are pretty similar to objects, which of course raises the question, when

2
00:00:09,270 --> 00:00:10,910
should you use which?

3
00:00:10,920 --> 00:00:18,690
So let's compare the two. Maps can use any values and any types as keys, so you can have an object as

4
00:00:18,690 --> 00:00:25,860
a key, a string, a number and in the same map, you can have mixed key types. Objects on the other hand can

5
00:00:25,860 --> 00:00:30,810
have mixed key types but you are restricted to using strings, numbers or symbols,

6
00:00:30,930 --> 00:00:33,660
we'll learn about symbols later,

7
00:00:33,660 --> 00:00:35,450
so you have less flexibility there.

8
00:00:35,520 --> 00:00:36,000
You can't

9
00:00:36,000 --> 00:00:38,010
use a boolean as a key and an object,

10
00:00:38,010 --> 00:00:43,660
you can't use another object or an array. For large quantities of data

11
00:00:43,710 --> 00:00:50,250
and with that I mean more than 10 entries, really large quantities, maps can provide better performance

12
00:00:50,250 --> 00:00:53,310
than objects do. So that might be one possible use case

13
00:00:53,310 --> 00:01:01,080
if you're working with large amounts of key-value data which you need in your application and in many

14
00:01:01,080 --> 00:01:06,900
applications, you might never really have that much data but if you do have that, that might be a place

15
00:01:06,900 --> 00:01:12,500
where you want to use maps, whereas objects are perfect for small or medium sized sets of data.

16
00:01:12,540 --> 00:01:18,870
And with that, I really mean medium sized not in the sense of five key-value pairs but still pretty big,

17
00:01:19,010 --> 00:01:21,170
30, 40 key-value pairs,

18
00:01:21,180 --> 00:01:23,780
that's certainly not a problem for an object.

19
00:01:23,880 --> 00:01:29,340
Objects by the way will also have another advantage which we'll see once we dive deeper into objects

20
00:01:29,340 --> 00:01:30,060
in the objects

21
00:01:30,060 --> 00:01:35,100
deep dive module, you can have methods that interact with the data in the object,

22
00:01:35,100 --> 00:01:37,420
that's not really possible on maps.

23
00:01:37,560 --> 00:01:41,620
So if you have methods that need to do something with the data that's part of the object,

24
00:01:41,670 --> 00:01:44,040
that's a strong reason to use an object.

25
00:01:44,250 --> 00:01:48,810
Now maps also can yield better performance when adding and removing data frequently,

26
00:01:48,810 --> 00:01:55,110
so if you have some operation which adds key-value pairs and removes key-value pairs multiple times

27
00:01:55,110 --> 00:02:00,960
per second, then a map might be worth a look. Objects on the other hand in general are easier and quicker

28
00:02:00,960 --> 00:02:06,060
to create, not just because you have that shorter syntax with curly braces but also from a performance

29
00:02:06,060 --> 00:02:11,930
perspective, new map takes a bit longer than creating an object with curly braces,

30
00:02:12,150 --> 00:02:17,460
though as always, with all these performance things, it's always important to keep in mind that we're

31
00:02:17,460 --> 00:02:23,710
talking about differences which you won't feel in the majority of apps you're building.

32
00:02:23,760 --> 00:02:30,120
So in general, the summary I want to give you here, not just about maps versus objects but about arrays

33
00:02:30,150 --> 00:02:37,800
sets and maps and objects in general is that in probably around 98% of cases, you will work with

34
00:02:37,800 --> 00:02:44,520
arrays and objects. Sets and maps have their niche cases where they can shine for unique value which has

35
00:02:44,520 --> 00:02:49,950
to be unique in the case of sets, for large quantities of fast changing data

36
00:02:49,950 --> 00:02:57,120
in the case of maps or if you have some use case where you have some data, let's say an object, where

37
00:02:57,120 --> 00:03:02,580
you kind of want to attach extra data which you don't want to merge into the object, as we did it in

38
00:03:02,580 --> 00:03:08,430
this example I showed you in this module, then you could also think about using a map to use the object

39
00:03:08,460 --> 00:03:13,360
as a key and add the extra data as part of the map instead of as part of the object.

40
00:03:13,530 --> 00:03:19,950
But again in the vast majority of cases, you can focus on arrays and objects but since sets and maps

41
00:03:19,980 --> 00:03:25,830
exist and you might encounter them in other code and you might personally also end up in some application

42
00:03:25,830 --> 00:03:28,490
at some point of time where you can benefit from them,

43
00:03:28,560 --> 00:03:34,560
it's of course important to be aware of them and to know them. Well and with that, they're actually also

44
00:03:34,560 --> 00:03:38,880
are two specific versions of sets and maps which are also quite interesting.
