02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Variables Perform Better Together

Variables Perform Better Together

In this tutorial, we explore how grouping variables, localizing their scope, and leveraging explicit comparisons can significantly enhance the performance of your JavaScript code. With practical examples in React, you'll learn to write cleaner, more efficient applications.

Key Concepts Covered

  1. Localized Variables for Optimal Performance

    • Place variables as close to their usage as possible to reduce memory overhead and improve runtime efficiency.
    • Use block-scoped declarations (const, let) to ensure variables are confined to the smallest necessary scope, automatically freeing memory when no longer needed.
  2. Minimizing Definitions

    • Avoid unnecessary variable declarations. Instead, group constants and reuse values when feasible to reduce initialization overhead.
    • Example: Instead of defining multiple const statements, use a single const with grouped declarations for improved clarity and performance.
  3. Switches Over If-Else for Comparison

    • Switch statements are often faster than if-else chains when dealing with multiple conditions.
    • Learn to use switch logic to dynamically render React components like navigation elements, reducing conditional overhead.
  4. Creating Reusable Components

    • Build versatile components, such as navigation bars, by dynamically grouping related elements.
    • Implement features like menu items, brand logos, and other navigation elements in a modular fashion for better reuse and maintainability.
  5. React-Specific Enhancements

    • Use React utilities like props.children to dynamically filter and categorize elements based on their type.
    • Combine JSX and ES6 features to create flexible, high-performance UI components.

Practical Example: Building a Dynamic NavBar

The tutorial includes a step-by-step guide on building a dynamic navigation bar in React. You’ll:

  • Separate navigation items into categorized groups like brands, links, and others.
  • Optimize rendering by avoiding redundant definitions and leveraging React's declarative model.
  • Improve readability and maintainability of navigation components through systematic organization and localized variable use.

This session concludes the deep dive into variable optimization, setting a strong foundation for advanced React development.


Why Continue the Course?

Optimizing your variables is just the start. The full course on React Performance Enhancements takes you through advanced techniques like string optimization and conditional rendering, ensuring your applications are fast, scalable, and maintainable. Take your React skills to the next level today!

Video Transcript

00:00In the last few lectures,
00:02we were taking a deep look into when
00:04to use the different types of definitions from VAR
00:07to let all the way through cons
00:09or even not using a definition at all.
00:11But now that we know that we have to define things at times,
00:14question is how do we set them up to perform at their best?
00:17And that's exactly what this lecture is about,
00:20where we're gonna talk about how it's important for you
00:22to localize variables
00:23and how you do that to increase your performance.
00:26By the way, this is mainly applicable to situations
00:29where you're gonna call a specific line of code.
00:31Often if it's through uh, listener
00:34or it's through a loop,
00:36creating these localized solutions will improve your
00:39performance dramatically.
00:41So we're gonna talk about that, the concept
00:43of minimal definitions as well, localizing your variables
00:46and as we're at it how you really should wanna make sure
00:50that you're explicitly comparing variables
00:52and not using loose comparison
00:54because that costs you in performance as well.
00:57So really by the end of this lecture we're gonna wrap up the
01:00big picture conversation about variables
01:03and how to work with them.
01:06As I was saying in the last lecture, one
01:08of the most important things we're talking about performance
01:11is balancing out with usability
01:14and no more so could be said about building the nav bar
01:17and let's go ahead and do that
01:19and to be able to build the nav bar to sample it
01:23and showcase what I mean.
01:24I'm gonna go ahead and first of all, the first step I need
01:27to do is I also need to include in our CSS
01:29that is gonna be imported
01:31or in this case our sas that is gonna be then
01:34encoded into CSSI need to also load in the nav bar.
01:38So go done. We've loaded in the nav bar.
01:40Now that we've done that, let's go ahead
01:42and also create the nav bar itself.
01:43So after creating the nav, just go ahead here
01:46and create the nav bar as well.
01:49On contrary to a regular Navion element,
01:52the nav bar itself has three distinct type of things
01:56that could be rendered within one of them.
01:59And if we take a look a deeper look into our,
02:02into the documentation here, one
02:04of them is a brand which basically enables us to push in uh,
02:07the brand of our element, which could be a link
02:10or could not be a link.
02:13After that, it has a menu element, which in which is man,
02:16not mandatory either or the brand doesn't have
02:19to be mandatory, nav doesn't have to be mandatory.
02:21And last but not least is actually anything else
02:23that we want me assign afterwards.
02:26Um, and to be able to do that, I need to be able
02:28to differentiate between them
02:29because what I wanna do is I want to go inside
02:32of my app itself and as I'm gonna create it into a nav bar,
02:36what I'm gonna do is I'm gonna go ahead
02:38as I turn it into a nav
02:44Bar and lemme go ahead and also close it as a nav bar.
02:47What I'm gonna do is if it's a nav link, great,
02:49that means this is something
02:50that should be a navigational link.
02:52But I'm gonna also wanna create here something
02:55that is not a navigational link
02:56because when we're looking at our code, again,
02:58if we look at the sample then we really wanna have an option
03:01for a navigation or a brand item.
03:03So to do that, let's just go ahead really quickly
03:05and create a nav bar brand item.
03:07And I'm just gonna go ahead into our navigation,
03:10create your a nav link.
03:11After our nav link. Let me just duplicate our nav link
03:14and I'm just gonna go ahead
03:16and use that as our nav
03:20Bar brand.
03:22Now our nav bar brand is literally all that needs
03:25to do is something incredibly simple.
03:27All it has to do is send in the hrif. That's still good.
03:31The class name, we don't need anything fancy here
03:33because the class name
03:34unless uh, will later on need some sort of extra fanciness.
03:38All we really need to do is make sure
03:40that it has the actual name that it should have,
03:42which is the nav brand.
03:44And let me copy that and put there nav bar.
03:50Last but not least, I'm gonna set you the Arif.
03:53I'm just gonna set you a standard A if I don't need this
03:55extra fun things and children works perfect freely as well.
03:59So now I have a nav bar brand.
04:01Lemme just go ahead save it in my app.
04:03I'm just gonna go ahead and also add that
04:06beyond the nav link.
04:07I also want to have access to the nav bar brand.
04:09Oopsie, I deleted that instead
04:11of adding just nav nav bar brand.
04:14And now I could go ahead and just add that bar brand.
04:16So I'm gonna go into my link here,
04:18add another navigational element
04:21and this time around I'm gonna have a nav bar brand here
04:24and it's gonna be uh, close here and let's give it a name.
04:29And our name is gonna be O2 geek
04:32and our link will
04:34be Beautiful.
04:39So I have my nav bar and that is my nav bar.
04:41Now technically if I just go ahead
04:42and look into my application,
04:44it should actually join into our application,
04:46but we, we still have navigational issues here among them.
04:50One of our issues is that this is not actually a menu item,
04:54it's not sitting in a menu item, it's just menu elements.
04:57We need to address that. And that's exactly the point
04:59where in our case, lemme just go ahead here
05:01and also add something that is not menu related.
05:04I'm gonna go ahead here and add here and input and,
05:08and I'm just gonna type here.
05:10Hello, let me save that
05:12and I'll have your just some sort of something here just
05:14so I'll have some sort of a navigational element,
05:16although maybe input is not a valid HTML tag.
05:20Um, it can't have children either,
05:21so let put your own input without children.
05:25Okay, so I have your own input
05:27and we have your some sort of an input element,
05:29which is coming all the way in the end here.
05:31Technically, I, I, I want it there, that's fine.
05:35But my problem is, is that I want
05:36to extract out our navigational element
05:38and place it in the middle here as a nav item.
05:41Now to be able to do that, unless I'm gonna wrap this inside
05:44of a nav, which I don't want do for the sake
05:46of this example, what I really want to do is I want
05:49to basically go through each element that is coming
05:51through in this rendering process
05:53and as I'm going through it
05:55to basically filter the items down into three areas
05:59where I want them to be.
06:01So I'm gonna go ahead. First of all, as always,
06:02we always start with a cons.
06:04One is our brand items.
06:06So I'm gonna go ahead and set it as uh,
06:08an array from a performance perspective by the way,
06:10and also from a general best practices in JavaScript,
06:14you always want to define all
06:15of your variables in the top here.
06:18Now that I've set it, set them on the top here instead
06:20of setting cons, a separate cons in each separate line,
06:23what you wanna do is you wanna basically just set a comma
06:26enter and continue on.
06:28'cause then the compiler
06:30and oto a real browser will know
06:32that everything going on here is gonna be constant.
06:35So I don't have to check what it is in each line.
06:37I know it's constant. These are small things,
06:39but did you improve on performance?
06:41Last but not least is our other,
06:43and we could also, I'm setting it in advance as a rate
06:46'cause we're gonna be placing content within,
06:48uh, technically speaking.
06:49Um, I also don't want to have another one here
06:51so I could set in my class name separately as well.
06:54Uh, in conjunction with all the other cons.
06:57I'm not gonna use let
06:58because I'm not actually changing anything here.
07:00These are all gonna be constant constants.
07:04On contrary, I'm gonna go through now
07:06and loop through all the children of the item.
07:09In this case, my item that I want to use is gonna be a lead
07:12because it's gonna change in every round of our loop
07:14and explicitly I'm defining it inside of the loop.
07:17'cause once we're out of the loop,
07:18we don't want it to be defined anymore.
07:21Now generally speaking, which loop should you use
07:23is not that critical.
07:25Although in our scenario, if I only need
07:26to use the actual items that actual children,
07:29I don't need anything else beyond that.
07:31So I'm gonna go to the most explicit, which is new
07:33to E es six, which enables me to basically say
07:37of this props children
07:39and it'll then send me child
07:40by child into the item without needing to actually go
07:43through uh, through an object.
07:45'cause anytime you call an object, you're actually
07:49affecting performance lightly more about performance.
07:52Now if conditions, generally speaking, take more
07:56on the system than switches and in our case,
07:59because I really don't need an if anytime you could use a
08:02switch, although a lot of people
08:03don't use them often enough.
08:05But anytime that something could be done in a switch,
08:07then use a switch.
08:09In this case, if I go ahead here
08:11and I'm gonna say my item type
08:13and I'm gonna switch through my item type.
08:15And in my item type I'm gonna go and list out the cases.
08:19So when my item type equals nav link, beautiful,
08:24whenever it's a nav link, what do we wanna do?
08:26Well I want to push into my um,
08:28item which is a menu item more accurately.
08:31I wanna push into it the item
08:32that I've just re what did I do?
08:35Oh, this should be items, let's call it nav.
08:41Let nav items.
08:43Sometimes names need to be explicit
08:45'cause they could become confusing.
08:46Okay, so nav items I'm pushing in.
08:48I wanna break out once I've done that.
08:52Okay, next case, next case is in a situation
08:55where we're actually sending in a nav bar brand.
08:58So there you go. In that case I'm gonna go ahead
09:01and I'm gonna create here a,
09:05I'm gonna push into it, that item.
09:07Okay, last but not least before.
09:10Let's break Last
09:12but not least, anything else that is not captured
09:14by default, we want to set it inside
09:17of our other Beautiful.
09:21And there we go. We've just used a switch.
09:23Instead of using an if condition, we set our let,
09:26which would improve our performance slightly as well.
09:29If we really wanted to create something highly
09:32with a higher improved performance, which should we do,
09:34we always do, which we'll talk about
09:35that in the next lecture.
09:38But before we do that, let's go ahead
09:40and also we'll get this to work.
09:41So once we've set it and we defined it, all that's left
09:43for us to do is actually go into our, uh,
09:46building of our item here.
09:47And instead of sending in our children,
09:49we're not gonna send the children in.
09:50First of all, we're gonna send in our brands.
09:54If there are no brands
09:55and no rendering, we'll actually commence.
09:57The next thing is I'm gonna go ahead and create a new nav.
10:00'cause I could assume that we have your navigational element
10:03and I'm gonna make sure that I have the nav bar nav CSS
10:06class to make sure that our nav element knows
10:09that it's gonna be a nav bar element.
10:14And I'm gonna also make sure that I'm gonna push into it,
10:17it all of the nav items.
10:21And last but not least, I'm gonna package
10:23it all up with the other.
10:24And there we go. We just created the sandwich that we need
10:27to create dynamically this navigational bar
10:30that is our um, nav bar.
10:34But we want one more thing that we have
10:35to do is we actually are not gonna use the class here,
10:38but we are gonna need this classify logic here.
10:41But instead of that classify that is right here,
10:44I'm gonna go ahead and just assign it directly inside
10:47of here because what I want to do is I want to go ahead
10:49and set this to be nav bar
10:56And let me double check if there's anything else.
10:58No, I think we are good.
10:59So if I save this, do I, do I need to change anything here?
11:02No, everything here was changed already.
11:04Make sure that everything is loaded. I believe it is.
11:07And if we go back and we take a peek at our application
11:09again, now our navigational element is actually building out
11:13as a navigational element,
11:15but our outcome is almost good but not that good.
11:18Obviously we're not really getting the menu as we wanted
11:20because if we, as I'm looking, I see first
11:21of all we have your extra stuff such as nav here
11:24and undefined, which we'll talk about really briefly.
11:26But beyond that we also forgot, I forgot
11:29to add also a nav item.
11:30'cause anything that is a navigational item
11:32needs to be a nav item.
11:33So let's address that and to address that,
11:35I'm just gonna go ahead here
11:37and um, I'm already in the right line here.
11:38And let just add your item space, save it
11:41inside of our nav link.
11:43And now that I've done that,
11:44our navigation should look much better, which is exactly
11:48how it should look like.
11:49Now we're far from done with this menu, don't worry.
11:51We're still continue working on it
11:53as we talk about enhancements
11:55to performance overall in React.
11:59So in this lecture we really took a step deeper into the
12:02world of how to work efficiently,
12:04high performing variable definitions.
12:07In the next lecture we'll continue into another type
12:10of variable, which is strings.
12:12And what is the best way to work specifically
12:14with strings in ees six.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!