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

Create a Const Only When You Need It

Create a Const Only When You Need It

Understanding when and why to use constants (const) is pivotal to writing high-performing JavaScript applications. This tutorial dives into the nuanced decisions that help balance performance and reusability, with practical examples in React development.

Key Takeaways:

  1. The Pitfalls of Over-Planning
    Over-defining variables and constants can lead to excessive memory consumption and performance bottlenecks. The lesson emphasizes focusing on minimalism—using variables only when absolutely necessary to avoid "over-planning."

  2. When to Use Constants
    Constants are best utilized when a value needs to be referenced multiple times or shared across components. However, if a value is only used once, defining it directly within JSX or function calls is often more efficient.

  3. Simplifying Components
    Learn how to streamline React components by leveraging utility functions like classify to dynamically assign class names. This approach reduces repetitive code, improves readability, and minimizes opportunities for error.

  4. Performance vs. Usability
    The tutorial highlights scenarios where optimizing for performance might slightly compromise usability, and vice versa. For example, removing unnecessary constants improves memory usage but might obscure code clarity if not managed thoughtfully.

  5. Practical React Navigation Example
    Follow the hands-on example of building a reusable NavLink component. You'll learn how to simplify links with defaults, streamline JSX, and make navigation elements easier to reuse across applications.

This lecture not only covers the technical foundations of managing variables but also integrates performance considerations into real-world React development scenarios.


Why Take the Full Course?

This video is part of the comprehensive series on ES6 performance enhancements in React, where you’ll master modern JavaScript techniques to build faster, cleaner, and more maintainable applications. Want to learn more strategies for optimizing your React projects? Enroll in the full course and take your development skills to the next level! 🚀

Video Transcript

00:01One of the biggest problems in my perspective developers
00:04have is that they tend to overplan
00:06and over-planning usually means too much variables,
00:10too much code that ends up to be a performance hug.
00:13On the contrary, if you think small
00:15and you don't over build, you'll actually find out
00:18that your code will be highly optimized by default
00:21and it's a really good strategy.
00:23So we're gonna take a look at that concept about
00:25how creating variables in constants
00:28whenever you don't need them really affect negatively
00:30your memory usage.
00:32Now generally speaking, uh, the rules that I like
00:35to apply are when do you want to use a constant is
00:39whenever you need to use the same thing more than once
00:42or you need to send the information
00:44from one place to another.
00:45And then in that fetch process you have
00:47to put it into some sort of a bucket.
00:49And really only in those scenarios do you actually move into
00:52a constant or a variable for that matter.
00:54So in this lecture we're gonna take a look at
00:56that decision point that you're gonna make here
00:58between improving your performance versus
01:01improving your usability.
01:02'cause end of the day, if your code is not usable,
01:04well then again it's gonna hit on your performance over
01:07time and vice versa.
01:08So it's always a game of balancing.
01:10So anything really that I'm saying in this lecture could be
01:12applied differently depending on the situation,
01:15but you're gonna get it really, really well in this lecture.
01:19Now a big element of performance is also building out your
01:23components in a way that they're usable in the most easier,
01:27easiest way to work with them
01:29and what does it actually mean?
01:30Literally speaking, if I just had my component do this,
01:34it would not really help me much as a developer
01:37because every time I would have
01:38to use the navigational element, I would have
01:40to remember a lot of things
01:41and I would have to type a lot
01:43of things which are both a big negative.
01:45That's part of the reason why in our navigational element
01:48and throughout our component elements, we're using classify.
01:51Because what classify enables me to do is literally
01:53to delete that nav class name because that is a default.
01:57Every single navigational element has a nav
01:59and we'll automatically update
02:01and make sure that it's
02:02inside there if you look into the outputted files.
02:05But let's continue down this path.
02:07We absolutely do not want to type so much
02:09whenever we're creating navigational links.
02:12So to do that, what I'm gonna go ahead is I want
02:14to create another class and I'm gonna go ahead
02:17and export that class as well
02:19because we're not using defaults, I'm just gonna go ahead
02:21and export that as a nav link.
02:24And I'm gonna go ahead and classify it as a nav as well
02:27because N link elements also need to start with have
02:32that appendixes of nav.
02:34So with that said, what I'm gonna go ahead
02:36and do is, what I wanna do is in this navigational element,
02:40besides this sending of this information, I'm gonna go ahead
02:43and let me just grab one link, uh, to be used as an example.
02:47And I'm gonna go ahead into my code here.
02:50And instead of this navigational element,
02:53really when you think about it,
02:54it's literally the same thing.
02:55The only difference is, is for one is
02:58where you'll have a a a tag instead.
03:01So let me, I could also delete all this just
03:02to put it all in one line 'cause it's so simple.
03:05But what I could do also just for the sake
03:07of talking about performance, so
03:08what we've just done this just by doing this,
03:10we automatically improved our process.
03:14Yet what one thing that we want
03:15to also do is if every single element is gonna have a link
03:18in it, let's just go ahead and add that as a default.
03:21So every single navigational element will have that link,
03:24so we don't even need to add it.
03:27Now to do that, as we were saying,
03:28the classify function enables me
03:31to basically add items in to the element.
03:34So what to dynamically, basically any place
03:37where you have your just a dash,
03:39it will automatically add the nav into it
03:41as it's doing it already.
03:42Anyways, I'll go ahead and add it in.
03:45Now, one thing that I wanted to note is generally speaking,
03:48this is a preferred way of concatenating strings.
03:50It's a preferred way versus actually going to a string
03:54and appending items into it.
03:56So if I wanted to append something,
03:57I would not call a method
03:58because a call of a method is more pricely than actually
04:02combining two uh elements together, two strings together.
04:06So that's one performance enhancement tip
04:07that I could already give you on the spot.
04:09Now beyond that, in our case,
04:11because we're only using one time, our constant,
04:15it is a little bit of an overkill
04:17to define it instead of defining it.
04:19And because we're only calling one method in one element,
04:22I could literally just go ahead
04:24and inside of my JSX, just send in that method call.
04:27This method call might not look very pretty,
04:30but this is will perform just slightly better
04:32because we don't have a need even for a reference.
04:35If we were gonna use this more than once,
04:37then absolutely we'd want to use the cons.
04:40So taking to the rules that we were talking about
04:42previously, you could use variables for the sake
04:45of clarity like we're doing here.
04:46It makes it cleaner, makes it easier for us to read.
04:49Although technically from a performance
04:51perspective, it's not really needed.
04:53It actually hinders a little bit performance
04:55'cause a little bit of memory needs
04:56to be stored for a constant.
04:58Now on this area, we don't need to add an extra item
05:01and thus if we don't need it to be incredibly easy
05:04to understand what's going on there,
05:06then in this case we just improved our performance slightly.
05:09Now with that, that said, let's go ahead
05:11and test this, these changes that we made.
05:13Now I could go ahead and literally just get rid of any place
05:16where we have only a nav link
05:18and just get rid of the nav link altogether.
05:21And oh, by the way, uh,
05:23'cause we're using a nav item now,
05:25so let me just go ahead and we need to delete that.
05:27Okay, so we have very disabled, that's good,
05:29but we also wanna make sure
05:31that we're gonna import the nav link and less, but not least
05:34before we close this up, uh, we don't wanna,
05:37we wanna send an oto, this Arif.
05:39So let me go ahead and add here an Arif.
05:43And for our Arif, I'm literally just gonna pass
05:45through our props.
05:47ATF not set, not saving any variable,
05:50we're not modifying anything.
05:52And now that we've done that, we could go ahead
05:54and start using that nav link.
05:55So if I go inside of here,
05:57let me just change it into a nav link and I'll go ahead
06:00and do it to all the items here
06:02and close all the items up as well.
06:05And there we go. We're now we're using the nav link.
06:08All we have to do is also make sure
06:09that we're loading up our nav link as well.
06:11And now we have variables that even are better performing.
06:14And more importantly than that,
06:16we're also creating a navigational element
06:18that is a lot easier to reuse
06:20because it's very, it's a lot more minimalistic than
06:24what it was previously.
06:25So if I go ahead here, if I want
06:27to even make it even maybe more enhanced, I could set it
06:29to maybe if enhanced from a usability perspective,
06:33not necessarily from a performance perspective.
06:36I could then go ahead and say, okay, if there is no hrif,
06:38then um, let me set that hrif f
06:41or I'll set it up to be, uh, just a string there.
06:46So what I'm doing here is I'm maybe hindering the
06:48performance of the overall application,
06:50but I'm improving the overall usability of the component.
06:53'cause now if someone doesn't put an Arif, so for example,
06:56maybe this link will go to, uh, my website, zero to geek.com
07:01and maybe this link will go to my blog
07:07and maybe this disabled link will go into Twitter.
07:14But our less our only link, our one
07:16of our navigational elements, we'll just have your no link,
07:20no link provided inside of our navigational element.
07:23Now if everything worked and I save our navigation,
07:26save our files, go back into my browser
07:29and check it out, we should see now, which are up items
07:32updating our disabled item updating as well.
07:36And if I go ahead and I take a a peek and I, I take a gander
07:39and inspect the element itself.
07:41So let me just find it and it's not working.
07:44Why aren't you working? Let close that up.
07:46Let me just inspect element again. Let it refresh.
07:50And now I'm gonna go ahead
07:51and search for our navigational elements
07:53and inspect that item.
07:54What we should see now is if we
07:56find our elements, we'll see that.
07:57First of all, we can see that we have your small error.
07:59We're not, we don't have a space. Space is critical.
08:01So I'm gonna have to go back and fix a typo
08:03that I have in my navigation where I need
08:05to add a space here before,
08:07'cause I concatenated it with whatever item
08:09that is afterwards.
08:10And if I go ahead and save this
08:13and go back into my application, it should now have
08:16that space there and our disabled state
08:18should actually be disabled.
08:19But we'll also see that any place
08:21where we don't have a link, it will automatically put
08:23that link just like we said it.
08:24Now we're talking about performance.
08:26Sometimes it's about overall performance of an application,
08:29but in many times we're also talking about performance.
08:32When you're talking about reusability, uh,
08:34for the developer, the, the easier it is for the developer
08:37to not make mistakes, the better performing
08:38your application will be.
08:40It's kind of intuitive. So with that said,
08:42it's really always a question of balance between performance
08:45and reusability.
08:46So for example, in a scenario like this,
08:48is this better for performance?
08:50Yes, if I had your allotted code, I probably wouldn't do
08:53that because it would be harder for the developer
08:55to read this if they have to go inside and modify things.
08:58So with that said, we've just enhanced our
09:01performance slightly a little bit more.
09:02As we built out our navigational element in this lecture,
09:07we really sealed the deal
09:08with the conversation about variables, when to use VARs,
09:11let cons and so forth, and when to not use anything.
09:15And that really, uh,
09:17seals up our conversation in this topic.
09:19And in the next lecture, what I really want
09:21to talk about is, okay, now
09:22that we are creating these variables,
09:24how do we create variables
09:26that just perform better than they would naturally.

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!