00:00Through the case of improving arrays, we're gonna run
00:03through the concept of calling less functions.
00:06The less functions you call the better.
00:08And that is very true with arrays as well.
00:10In the process, we're gonna talk about how it's important
00:13to predefine the length of your array if you know
00:16what it's going to be and how you shouldn't use methods.
00:19What you could do, instead of using methods, which is
00:22to access instead of pushing,
00:24which is the biggest thing when we're talking about
00:26arrays, let's jump right into it.
00:27Let's see how we optimize arrays to the modern world
00:30of JavaScript.
00:33As with a lot of the other videos that we had,
00:35our focus on performance really
00:38is about generally conditions where you're,
00:41you're having issues of performance.
00:43Now, in this case, the tips I'm gonna give you now are good
00:46always and always try to use them.
00:47And it really doesn't matter what version of JavaScript
00:49or for that matter, any programming language, no matter
00:52what version it is, you're gonna, it's be a good,
00:54it would be a good idea for you to do this.
00:57Okay, so I wanna showcase you first the wrong way
01:00and probably the way that it was that some do it.
01:03So I'm gonna go ahead and what I wanna do is I want
01:05to create a, or an array just for the sake of this sample.
01:09And what I want do is I wanna loop through it.
01:11So first of all, I'm gonna create a four loop
01:13and I'm gonna skip the definition
01:14'cause I'm defining it once above in the good old way.
01:18And I'm gonna go ahead and set my I to be, as long
01:21as it's smaller than a hundred.
01:22And let's say I'm gonna add into my I five each round.
01:26And then I want to push into that array.
01:30Our value of I last
01:32but not least, let me just go ahead to my console.
01:36So log and log out the value in a, so this is a sample,
01:40and if I go into my, uh, browser
01:42and search for our array, we'll see that we have your,
01:45all these different values that are being outputted out.
01:47Now, generally speaking,
01:49whenever you're working with arrays, there are a few things
01:51that you really want to pay attention to for one of them.
01:54Don't forget that our rule,
01:55that anytime we call the function,
01:58we're probably doing something bad.
02:00Now, on secondary note, as we said,
02:02that generally we don't really care about that global scope
02:05'cause Babel and our processing of compiling, make sure
02:08that even if we put your variables that are
02:10supposedly global, they're not really global.
02:13But for that matter, for example, our, i,
02:15if we would just put your, I var I you're right,
02:18this would be less performance enhancement technically,
02:21theoretically, although not very big, um,
02:24than just setting it up in advance.
02:26On contrary though, if we set here a ette
02:29and we set here ette, what we're actually doing is though,
02:32okay, so the scope of this will be the scope above.
02:34Nothing really changed, but the scope inside of our
02:37for loop is gonna reduce that scope to live only within this
02:40for loop, which, which would be brilliant
02:42because we don't need this eye once we're done
02:44with this for loop.
02:46Now, moreover than that, um, let's go ahead
02:49and also talk about this a push
02:50'cause that's a big negative.
02:51Generally speaking, whenever you're working with a,
02:54with a four loop and you're working
02:57with an array, you really want
02:58To be able to predefine your size of your array.
03:02So for this matter, the easiest way for me to do this is
03:04to just go ahead and set a new array
03:06and declare in advance that it's gonna have a hundred spots.
03:09For example, if we would have a hundred,
03:11or really technically speaking in our situation,
03:14we would have here basically a hundred divided by five.
03:18So we would have roughly 20 spots.
03:21Let's say we have exactly 20 spots,
03:23and in the situation we have 20 spots.
03:24So I'm gonna predefine RRA to have 20 spots.
03:28And in that condition,
03:29what I would do is I would just go ahead
03:30and push my item one by one.
03:32So for example, in a situation,
03:33let's make it a more realistic for the sample
03:36of the, of this presentation.
03:38'cause here it would actually be much more complicated
03:40because we're not jumping one by one.
03:42But imagine that we're doing a normal loop,
03:44which usually we're, we're recycling through one by one,
03:47and we want to put items inside of this array.
03:50And a situation like this would be way better off.
03:52Just go ahead and set our a value
03:55and define its, I value into it directly.
03:58Now, if technically all we're trying
04:00to do is concatenate different string values to output,
04:02then we probably could use a string
04:03and not even use an array.
04:05But in our situation, what I'm basically doing here,
04:07I just cut the need to use a function calling a a property
04:11as a lot cheaper than calling a proper, uh, a method.
04:14And by that I'm reducing the, the,
04:16the complexity of our code.
04:17Even even in a situation where I have a hundred things
04:20that are running at the same time.
04:22Those minute little details specifically in these type
04:25of situations are gonna have a massive impact on
04:28performance related things.
04:29Now, I wanna talk about one more thing.
04:31When we're talking about performance.
04:33Imagine that we had here, uh, we wanted to, uh,
04:36concatenate twice from, instead of from zero to a hundred,
04:39we wanted to contaminate, let's say from zero to 50
04:42and then from zero to 50 again.
04:45Now, the problem with our code right now is multiple.
04:48Now first of all, we're defining an, uh, this let twice,
04:51which may be in theory,
04:53we'd be better off just defining that.
04:55Let once, uh, that let once defining that eye as well,
04:58and then defining that eye condition inside of here
05:01so we could reuse the same variable if it's used being used
05:04for the same type of purpose.
05:06Moreover than that, for example.
05:07Okay, so this is good if we just want to output from one
05:10to 50, once inputting that into our console.
05:13So if I go ahead and look at my output,
05:15I should see here all these values
05:17with 90 more going from zero all the way to 50,
05:21and then starting over again, going, going again to, to,
05:25to going through.
05:26But in our situation right here,
05:28we're basically starting off again from zero,
05:30which is again, a, a, a problematic feature for us.
05:34But though, technically that's exactly what I want to do
05:36because I would want to go ahead
05:37and cons this out right here.
05:38So I'll get my first 50
05:40and then cons it out again and get the second 50.
05:42Again, this is not a realistic example.
05:44It's more of a, these are the things that you want
05:46to be thinking about in this lecture.
05:48We introduce you to how to work
05:50with arrays in a more efficient way.
05:52We're not done talking with arrays.
05:53We want to continue the topic of array, taking a step out
05:56of specifically arrays to the concept of reusability,
06:00which we'll cover in the next lecture.