Check out a free preview of the full Professional JS: Features You Need to Know course

The "Proper Tail Calls" Lesson is part of the full, Professional JS: Features You Need to Know course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano introduces the last module of the course, which covers advanced techniques in JavaScript. He discusses proper tail calls, provides an example of a factorial function, and demonstrates the difference between a regular tail call and a proper tail call.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Proper Tail Calls" Lesson

[00:00:00]
>> Maximiliano Firtman: So, the last module is some advanced technique some things that you may not use on a daily basis. But actually with all these topics, we will cover everything that has been added to JavaScript since 2015, which is a lot but also, there are a lot of people asking for more.

[00:00:19]
And yeah, those people are probably getting into TypeScript or other things. But in this section, we will talk about some other small changes that might not be so useful, but they're there anyway. Dynamic import, that is actually a big deal. Proper tail calls, but also take some time to understand what's going on with that.

[00:00:39]
Proxies and reflect API, we'll cover that briefly. Tagged templates, I think that's pretty cool. It's a very underused feature that we have in ECMAScript these days. Then, WeekRefs and FinalizationRegistry that with the name, we don't really get what's that. So we'll get into that in a minute. And finally, some enhancement to regular expressions, okay?

[00:01:07]
So that's the deal for this part of the version, the last part of the version. So going back here, we are going to start talking about proper tail calls. So this is not actually a syntax change. This is something that appear in Negmax script six to improve performance when you're using the stack frame.

[00:01:31]
That is when you have recursion. For example, in this case, this function is a typical recursive function to calculate the factorial of a number, okay? This is how it looks like, you're just multiplying a number by its previous number. So if you say factorial of 5, it's gonna be 5 multiply by 4 multiply by 3 multiply by blablabla.

[00:02:02]
So it's recursion, okay? So this one doesn't have PTC, proper tail calls. What is that? Well, from ES6, if your function looks like this, it's going to use a different the engine, it's going to use a different way to execute that code, and it will be much faster.

[00:02:30]
I mean, if I execute this, I'm getting the same result, and we are not going to even see the difference. We need to measure time to actually do that. But I'm more interested in showing you how do you know what's the difference? Well, in this case the difference is that we are accumulating the value in an argument.

[00:02:50]
So in their recursion, we are accumulating in the value and here we are returning the value. So, in this case, this is actually doing this. So if you think about this when you look at this return, the recursion is not the first operation. What's the first operation here?

[00:03:12]
Multiply by, multiplication is the first one. So it needs to take n and then this one. So let's try to understand this, how this works. Let's look at the return. We call this with 5. So n is 5, so it's 5 multiply by 3 out of n minus 1, so 4.

[00:03:34]
Then we just goes 5 and now it says 4 multiply by 3, okay? And you understand how this continues, 3 multiply by 2 and so on until you get to 1, okay? So it's fine, it works, but it's not so performant. So the idea of a tail call means that the tail of the return should be the recursion.

[00:04:07]
Right now the return tail is the multiplication operation. Now, if you look here back, when we are returning, we are returning a call to the same function. There is no other operation around. That's a proper tail call that return that's the tail. The return has no other operation than calling the same recursion function.

[00:04:35]
So this is not an API. This is not a new syntax in the language. This is like, if you have a situation like this, Deangei enters a different mode known as proper tail calls, and it's going to be executed much faster. Just that., okay? Makes sense? So it's just another way to create your recursion.

[00:05:00]
Anyway, unless you're doing functional programming in JavaScript that is actually pretty cool. And encourage you to check some other workshops in front of masters that will get you into functional programming with JavaScript, you probably are not going to use things like this.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now