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

Improving Arrays

Task 3: Structured Tutorial from Transcript


Tutorial: Why ES6 Isn’t Always Faster

Introduction

In this tutorial, we delve into the performance aspects of ES6 versus ES5, exploring why newer doesn’t always mean faster. We’ll look at specific scenarios where ES6 might lag due to browser optimizations or Babel transpilation. Additionally, you’ll learn practical strategies to optimize JavaScript performance.


Key Takeaways

1. Why ES6 Isn’t Always Faster

  • Browser Support and Optimization:
    ES6 features may run slower in some browsers because they aren't fully optimized yet. For example, transpiled ES6 code (via Babel) often results in more verbose, less efficient ES5 output.

    • Example: Classes in ES6 may perform slower due to the additional transpilation overhead in Babel.
  • Future-Ready Code:
    Despite current limitations, coding in ES6 is recommended for future compatibility. Performance will naturally improve as browsers evolve.


2. Optimizing Performance

  • Focus Areas:
    Performance optimization should target frequently executed code:

    • Functions: Optimize functions that are called often.
    • Loops: Reduce the complexity of loops or eliminate them when possible.
    • Conditions: Simplify or replace conditions (e.g., use a switch-case or mapping instead of multiple if-else statements).
  • Caching Results:
    Store results of expensive computations to avoid redundant processing.

  • Localizing Variables:
    Use local references for repeated accesses to improve lookup efficiency.

    • Example: Assign a frequently used object property to a local variable inside a loop.

3. Specific Features and Their Performance

  • Arrow Functions:
    These provide cleaner syntax and maintain the enclosing scope (this). Performance is similar to regular functions in most scenarios.

    • Example: const add = (a, b) => a + b;
  • Classes:
    Classes are convenient for React and modern frameworks but may perform worse than traditional function-based prototypes in certain environments.

    • Recommendation: Use classes unless profiling indicates a significant bottleneck.

4. When to Prioritize Performance Over Modern Syntax

  • If your app includes highly frequent DOM updates or intensive calculations, consider fallback optimizations, such as avoiding transpilation for specific modules.
  • Example: Rendering navigation elements hundreds of times per second might benefit from avoiding classes in favor of plain objects.

2024 Note: Embrace React Hooks

When working with React, hooks like useMemo and useCallback can optimize re-renders and state updates, reducing the need for manual caching and improving component performance.


Conclusion

ES6 brings modern syntax and developer productivity improvements, but performance isn't guaranteed to be better than ES5 in every scenario. By strategically optimizing frequently executed code and embracing best practices, you can ensure that your applications are both future-ready and efficient.

For more React and JavaScript performance tips, visit the course: React Performance Tips.

Video Transcript

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.

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!