1
00:00:01,080 --> 00:00:01,620
Welcome back.

2
00:00:02,070 --> 00:00:09,060
Let's talk about the very first roll when it comes to big oil, that is worst case when calculating

3
00:00:09,060 --> 00:00:13,200
big oh, we always think about the worst case.

4
00:00:14,070 --> 00:00:15,060
What do I mean by that?

5
00:00:15,970 --> 00:00:18,100
Well, if we go back to Finding Nemo example.

6
00:00:20,850 --> 00:00:23,040
And the Find Nemo function.

7
00:00:24,190 --> 00:00:29,830
You may have been screaming at me while I was going through the function saying that, Andre, this

8
00:00:29,830 --> 00:00:31,040
is not efficient.

9
00:00:31,060 --> 00:00:31,830
What are you doing?

10
00:00:32,560 --> 00:00:39,940
Because if you look at this function, we're looping through the entire array to find Nemo.

11
00:00:41,450 --> 00:00:49,010
Remember how we had the everyone tray with about 10 different characters from Finding Nemo?

12
00:00:50,230 --> 00:00:59,200
Well, Nima was only the one, two, three fourth member on this array, and when we run this function.

13
00:01:01,060 --> 00:01:10,700
We found Nemo, but the funny thing is this function ran 10 times, not four times.

14
00:01:11,350 --> 00:01:15,580
If I do a console log here and I say.

15
00:01:17,570 --> 00:01:20,330
Running and let's click this.

16
00:01:21,620 --> 00:01:29,010
We see that this function ran one, two, three, four, five, six, seven, eight, nine, 10.

17
00:01:29,660 --> 00:01:29,890
Hmm.

18
00:01:30,260 --> 00:01:32,100
I mean, we already found nimo.

19
00:01:32,120 --> 00:01:35,300
All these runs are pretty wasteful.

20
00:01:36,360 --> 00:01:43,530
So we can make this function a little bit more efficient in JavaScript, we can just have something

21
00:01:43,530 --> 00:01:45,870
called break.

22
00:01:48,140 --> 00:01:54,980
And again, different languages have different ways of exiting out of a loop if a condition is met.

23
00:01:55,010 --> 00:01:58,910
In our case, if we find Nemo, just break out of this loop.

24
00:01:59,570 --> 00:02:02,420
If I do this and click run.

25
00:02:03,390 --> 00:02:08,760
Look at that, once we found Nemo, we're done, we're not going to loop through the rest of the items.

26
00:02:09,360 --> 00:02:13,530
Congratulations, we just made our code a little bit more efficient.

27
00:02:14,350 --> 00:02:15,240
That's a good thing, right?

28
00:02:16,350 --> 00:02:23,850
Well, when it comes to Big O, although this is important and when you write good code, this is something

29
00:02:23,850 --> 00:02:27,030
that we want to do in the big scheme of things.

30
00:02:27,120 --> 00:02:30,540
Bigo only cares about the worst case.

31
00:02:30,990 --> 00:02:32,300
What is the worst case here?

32
00:02:32,610 --> 00:02:40,260
Well, the worst case is that nimo, instead of being the fourth item is at the very end.

33
00:02:43,400 --> 00:02:51,080
So even if we have this brake statement, we're still going to run this 10 times because nimo at the

34
00:02:51,080 --> 00:02:56,300
end, best case is if Nemos at the very beginning and we only have to live through it once.

35
00:02:56,570 --> 00:03:00,860
But worst case, we're still going to have to go through 10.

36
00:03:01,910 --> 00:03:13,880
Loops still big o of nd so this is our very first rule, the very first rule is we always care about

37
00:03:13,880 --> 00:03:18,830
what is the worst case scenario, because when we talk about scalability, we can't just assume things

38
00:03:18,830 --> 00:03:28,310
are going well, even though the Find Nemo function might be of one if Nemo.

39
00:03:29,470 --> 00:03:31,390
Is the very first item in the array.

40
00:03:32,740 --> 00:03:39,130
It doesn't matter in the grand scheme of things, because we can't be certain of what the input is going

41
00:03:39,130 --> 00:03:45,940
to be, we're going to assume that of Bigo is of non linear time.

42
00:03:46,860 --> 00:03:50,220
Again, if we're finding a yellow box.

43
00:03:51,300 --> 00:03:57,030
And the yellow box is at the very end, we have to iterate through all the boxes.

44
00:03:58,220 --> 00:04:04,130
If the yellow box at the very beginning, you see that we don't have to iterate through these boxes.

45
00:04:05,170 --> 00:04:11,980
But Bigo doesn't really care, even though this function, yes, is indeed more efficient, at the end

46
00:04:11,980 --> 00:04:16,000
of the day, when we talk about Bigo, we're talking about worst case.

47
00:04:17,010 --> 00:04:23,790
All right, I hope you got that point, too, I feel like I've beaten to a pulp, so you should be comfortable

48
00:04:24,000 --> 00:04:24,750
with it by now.

49
00:04:25,050 --> 00:04:27,390
Let's talk about the next role on the next video.
