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

Reusing Instead of Creating

Task 3: Structured Tutorial from Transcript


Tutorial: Improving Arrays for Performance in JavaScript

Introduction

Arrays are a core part of JavaScript programming, and optimizing their usage can significantly enhance performance, especially in scenarios with large data manipulation. This tutorial will guide you through best practices to handle arrays efficiently, focusing on reducing function calls, predefining array sizes, and avoiding common pitfalls.


Key Takeaways

1. Minimize Function Calls

Function calls, even for common array methods like .push(), can slow down your code.

  • Why it matters:
    Each function call involves overhead. Instead of dynamically pushing items, directly assign values to array indices.
    • Example: Replace array.push(value) with array[index] = value;.

2. Predefine Array Length

When possible, declare the array size upfront to avoid dynamic resizing during runtime.

  • How to do it:
    Use new Array(size) to create an array with a fixed number of slots.
    • Example: For an array expected to hold 20 items:
      let array = new Array(20);
      for (let i = 0; i < 20; i++) {
        array[i] = i;
      }
      

3. Optimize Loops

  • Use local variables to reduce scope and improve loop efficiency.
    • Declare loop-specific variables using let to limit their scope.
  • Combine initialization and reuse variables for nested loops or repeated operations.
    • Example:
      let array = [];
      let i;
      for (i = 0; i < 50; i++) {
        array[i] = i;
      }
      // Reuse i for another loop.
      for (i = 0; i < 50; i++) {
        array[i + 50] = i;
      }
      

4. Avoid Redundant Operations

  • Analyze the logic to avoid redundant computations or unnecessary concatenations.
  • Example: Instead of looping twice to achieve the same result, consider consolidating operations.

Practical Example

Inefficient Code:

let array = [];
for (let i = 0; i < 100; i++) {
  array.push(i);
}

Optimized Code:

let array = new Array(100);
for (let i = 0; i < 100; i++) {
  array[i] = i;
}

When to Use Strings Instead of Arrays

In cases where you're merely concatenating data, consider using strings instead of arrays.

  • Why: Strings avoid the overhead of managing an array's structure.
  • Example:
    let result = "";
    for (let i = 0; i < 100; i++) {
      result += i;
    }
    

Conclusion

Efficient handling of arrays involves minimizing function calls, predefining sizes, and structuring loops thoughtfully. By applying these practices, you can achieve significant performance gains in your JavaScript applications.

In the next tutorial, we’ll explore the concept of reusability and how it applies to arrays and beyond.

For more resources on JavaScript performance, visit React Performance Tips.

Video Transcript

00:01Creating things is a complex process for,
00:03uh, any language to do.
00:05'cause when you're creating an object,
00:07you're creating an object.
00:08You're cre, you're calling a constructor.
00:10There's a lot of things that are happening behind the hood
00:12that automatically would be more complex than actually
00:15reusing something that already exists.
00:17So that is really the core premise,
00:19and it's true to any language really,
00:21but in JavaScript even more explicitly.
00:23So in this lecture, we're gonna take a look at
00:25how could we reuse an array, such as
00:27how do we reset an array instead of starting over
00:30with creating a new array?
00:32And that will give us, to lead us to the whole concept
00:34of reusing oto objects as well.
00:36So let's jump right into it. So I just have one more tip
00:40about array that really takes us into a tip about objects.
00:42In general, the creation of objects takes time
00:46and it's takes a lot more time than accessing things
00:49as intuitively you probably would
00:51understand in this situation.
00:53So reusing this array is incredibly effective way
00:56of cutting down the amount
00:57of time it takes our application to be built.
00:59Especially if you're running on a server,
01:01or especially if you're running in a JavaScript application
01:04that is refreshed and changed often
01:06and has a lot of, uh, a high refresh rate.
01:09But what would happen in a situation, for example,
01:11where your next, uh, four loop, first one, fine,
01:13you wanted 50 items, but the next one you only want 20.
01:16If I just changed it to 20,
01:18my arrays length would still be 50
01:20and I would still have a problem.
01:22A really great way for us to reset our array or, or,
01:25or trunk it down very easily
01:27and very effectively is literally just going to the length
01:30of the item lens and just setting it to 20.
01:34If I wanted to reset it completely, I could just set it
01:36to zero and then it would be empty.
01:38And in that scenario, I would be building it each time in a,
01:41uh, each time, which would be bad, bad, bad.
01:44Why would that be bad? Because if it's not a set size,
01:46every time I'm gonna add an item, my array will need
01:49to be resized, which is an operation that needs
01:52to happen behind the hood.
01:53But in this case, what I'm actually doing is
01:55I'm resizing it once.
01:56Now my array's length is only 20.
01:58So for example, in this case, we don't need a hundred.
02:01My array was 50 initially. We went through the 50.
02:04We then chunk it down to be 20.
02:06We're updating all that data there
02:07and then tracing it again.
02:08And what would happen is my content would then,
02:11I would present it with my array first with 50 items
02:14and then with 20 items,
02:15which is the best thing for your performance.
02:18Now, when I'm speaking about that, about
02:20that creation process, that is so true also with objects.
02:23So if you're creating an object inside of an array
02:26and then creating in a loop, uh, you're doing a loop
02:29and you're creating 20, 30 objects
02:31and you're using them within that loop,
02:33and then you don't need them anymore,
02:34that is really bad usage of performance.
02:38You wanted to do something that is better performing,
02:40create it once, and then reset its data each
02:42time in that four loop.
02:44'cause that is highly more efficient than actually
02:47recreating an object each time.
02:49So generally speaking, you're talking about performance.
02:51Think about how many times you're creating an object
02:54and how reusable could your arrays that you're creating,
02:57objects that you're creating,
02:58Or even other items that you're creating,
03:01real classes that you're creating.
03:03If you could reuse them in a situation, mainly, A lot
03:06of times when we're talking about performance,
03:08we're talking about ORs thing that's living on the server
03:11or something that is gonna be rerun multiple times.
03:14So there you go. And that's really the biggest,
03:15most important talk about the things you wanna focus on,
03:19especially on the server all the time.
03:21But same is Otto on the client.
03:23In situations where something is happening, often, namely,
03:26like we're talking about often
03:27and often functions are called, often are raised inside
03:30of those functions, uh, loops inside of those functions,
03:33those are points of Achilles hills of many applications
03:36that you could very easily improve performance
03:38very dramatically within them.
03:41In this lecture, we learned how to reuse an array
03:44and recycle it instead
03:45of creating a brand new array each time.
03:48And with that said, we are done with arrays per se,
03:50because in the next lecture we're gonna move into the topic
03:53of immutability.

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!