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

Localizing let and const for Enhanced Performance

Localizing let and const for Enhanced Performance

This tutorial highlights the importance of variable localization in JavaScript and how effectively using let and const can improve performance. By minimizing circular references and optimizing accessor calls, you can create cleaner, faster, and more maintainable code.

Key Concepts

  1. Understanding Scope and Accessors

    • Accessing variables in a higher scope requires more computation. Minimize this by localizing frequently accessed variables.
    • Example:
      const props = this.props;
      console.log(props.className);
      console.log(props.children);
      
      Instead of repeatedly accessing this.props, localize it to props.
  2. Why Localizing Variables Matters

    • Every time you call an accessor (e.g., this.props), JavaScript resolves it through multiple steps.
    • Localizing reduces these steps, especially in loops or repeated calls.
  3. Avoiding Circular References

    • Circular references occur when variables in different scopes reference each other unnecessarily, causing memory leaks or inefficiencies.
    • Localized variables mitigate this by containing references within a specific scope.
  4. Using const for Immutable References

    • Always use const for variables that won’t be reassigned. This signals immutability and helps optimize performance.
  5. Optimizing Loops with Local Variables

    • Inside loops, avoid accessing higher-scope variables repeatedly.
    • Example:
      const children = props.children; // Localize before the loop
      children.forEach(child => console.log(child));
      
  6. When to Use let

    • Use let only when the variable needs reassignment.
    • Always prefer const when defining object references that will not change.

Practical Example

Suppose you’re building a React component:

Before Optimization:

render() {
    return (
        <div>
            {this.props.className}
            {this.props.children.map(child => (
                <div>{child}</div>
            ))}
        </div>
    );
}

After Optimization:

render() {
    const { className, children } = this.props; // Localize variables
    return (
        <div>
            {className}
            {children.map(child => (
                <div>{child}</div>
            ))}
        </div>
    );
}
  • This approach reduces redundant accessor calls, improving performance and readability.

Why Take the Full Course?

Learn the best practices for efficient JavaScript development with React. Gain hands-on knowledge to write optimized, scalable code in our full course at 02Geek React Performance Enhancements. Let’s code smarter, not harder!

Video Transcript

00:01So really this really concludes our
00:03conversation of variables.
00:04We're closing a circle back to a topic
00:06that we mentioned a few times about the importance
00:09of the scope of variables as we dedicate this lecture
00:12for local localizing your variables.
00:15The content behind it is about avoiding circular references,
00:18which forces our code to work harder than it needs to.
00:23So we're gonna really get familiar
00:25and understand the critical importance
00:27of how to build things out.
00:28So we're gonna, it's a wrap up lecture in many ways,
00:31but also an enhancement to all the points,
00:33gathering them up together, focusing on
00:35that localizing aspect.
00:37And let's go ahead and do that.
00:39It really goes ahead to that idea of saying the do less
00:43and as you progress only if you need it,
00:46then move into the next layer.
00:48In our case we're saying we're gonna start with a constant,
00:51we'll only then move to let whenever we need to.
00:53But beyond that oto, we're also gonna fetch the information
00:56and localize the information when by the end
00:58of this lecture, you're gonna really be comfortable
01:01with all these definitions
01:02that we talked about throughout this section.
01:05As I mentioned a few times already,
01:07every time you access a method, you're actually creating
01:11a performance disen enhancement.
01:13Really. And when you think about it,
01:15the process is very similar
01:16to the way you personally would look something up.
01:18So if you were looking for a function for, for example,
01:22okay, so it's in some sort of global scope
01:23and you grab that function on contraire, if I was trying
01:27to go to our navigation element, let's say
01:30to our nav bar render, now JavaScript has
01:33to find the nav bar and then find the render.
01:36Each one of these steps is very minute,
01:38but it does take time.
01:39Now, if you're already inside of a specific scope,
01:42for example, let's say we're inside of the nav link, I'm in
01:45that render link, we are already inside of it.
01:48Thus, if I'm going there
01:50and I'm saying, okay, I need to reference to this.
01:51Okay, it doesn't take that long because we're in there,
01:54but then I need to go to props.
01:55Okay, that's one. Then I need to go
01:57to class name, that's two.
01:59What I'm doing here is I'm basically, this is three calls,
02:01six calls, nine calls.
02:04Now, this might be a big difference if this is rendered only
02:06once in a while, but notice what happens if I set here cons,
02:09and I take the, I set it to be props
02:13and I set it to be this props.
02:15Now this has reduced our calls, basically called one, two.
02:20So I called two things, turned it into one.
02:23Now, anytime I could just go ahead
02:24and call, instead of this props, I would call just props.
02:28Let me just delete all of that and delete all of that
02:33and delete all of that.
02:35And this is a really big enhancement of performance.
02:38Any situation where you're calling
02:39something more than once, do it.
02:41Because if you do that, you're gonna improve performance,
02:43even in a scenario like this type
02:45of item when there's only two.
02:46Now, if we count this, we have here two, we have here two,
02:50we have here two.
02:51So it's 2, 4, 6, 8.
02:54Not a great enhancement, but
02:56because I created a local variable, it doesn't have
02:59to search for something in a variable above,
03:02so it's so much closer.
03:04And that's exactly the type of thinking you want
03:06to have when you're creating your elements.
03:08And generally speaking, I would say you're creating a
03:10concept of props in react.
03:12Just all the time. Do this.
03:14It's not a big, it's not a big hindrance on your development
03:18process, and it just will cut down your need
03:19to even use this once you've said it, that's it.
03:22You don't need to set it again.
03:23And now you have a very localized variable
03:25that you have access to more freely and more easily.
03:28So if I go ahead here and I check this out again,
03:30so here is this props class name.
03:32It's only used once. Am I using it again?
03:34Yeah, I'm using this props children in
03:36a loop, which is even worse.
03:38So lemme go ahead here,
03:39and let's just define up here our props,
03:42and it's gonna be this props, and I could just go ahead
03:47and deduct it a little bit further.
03:50Now, I don't need to use this props anymore
03:52because I'm just referring to these items.
03:54I'm not changing and I would never change anything in there.
03:56And last but not least, so, so not a great idea inside
03:59of a loop to uh, access an object.
04:02Although browsers and O2 servers are smarter
04:04and so smarter over time, learning how
04:06to cache things on their own.
04:07But if we're already at it, let's just go ahead
04:09and set a direct reference to the children.
04:12And I could just go to props children
04:15because we're gonna be using it inside of a loop.
04:17And for that sake, we just don't want to call an accessor.
04:21The less accessor we need,
04:22the less approaching other things, the better is off,
04:25better off, and the better end outcome we create.
04:28So generally speaking, that's one thing
04:30that you want to think about.
04:31Anytime you need to refer something more than once
04:34and try to create a local variable name more
04:37accurately, go for constant.
04:38Unless it's something that's gonna change,
04:39which if it's a just creating a local version,
04:42most probability it'll be a constant if it's an object,
04:45and that way you're gonna improve your performance even
04:47more dramatically.
04:51So that's it. This really concludes all
04:53of our conversation about variables.
04:55There's a few more topics that we wanna cover in this
04:57section, but we're done with variables.
05:00In the next lecture, we're gonna move on to a specific type
05:03of situation, which is,
05:04how do we avoid conditions altogether,
05:07or how do we reduce the calls for conditions?
05:09We're gonna use an object for that or a map,
05:12and we're gonna see how to do that as a way
05:14to improve your performance in specific scenarios.

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!