Reader small image

You're reading from  Professional JavaScript for Web Developers - Fourth Edition

Product typeBook
Published inNov 2019
Reading LevelBeginner
PublisherWiley
ISBN-139781119366447
Edition4th Edition
Languages
Right arrow
Author (1)
Matt Frisbie
Matt Frisbie
author image
Matt Frisbie

Matt Frisbie has worked in web development for over a decade. During that time, he's been a startup co-founder, an engineer at a Big Four tech company, and the first engineer at a Y Combinator startup that would eventually become a billion-dollar company. As a Google software engineer, Matt worked on both the AdSense and Accelerated Mobile Pages (AMP) platforms; his code contributions run on most of the planet's web browsing devices. Prior to this, Matt was the first engineer at DoorDash, where he helped lay the foundation for a company that has become the leader in online food delivery. Matt has written two books and recorded two video series for O'Reilly and Packt, speaks at frontend meetups and web casts, and is a level 1 sommelier. He majored in Computer Engineering at the University of Illinois Urbana-Champaign. Matt's Twitter handle is @mattfriz.
Read more about Matt Frisbie

Right arrow

TAIL CALL OPTIMIZATION

The ECMAScript 6 specification also introduced a memory management optimization that allows the JavaScript engine to reuse stack frames when certain conditions are met. Specifically, this optimization pertains to “tail calls”, where the return value of an outer function is the returned value of an inner function, as follows:

function outerFunction() {
 return innerFunction(); // tail call
}

Prior to the ES6 optimization, executing this example would have the following effect in memory:

  1. Execution reaches outerFunction body, first stack frame is pushed onto stack.
  2. Body of outerFunction executes, return statement is reached. To evaluate the return statement, innerFunction must be evaluated.
  3. Execution reaches innerFunction body, second stack frame is pushed onto stack.
  4. Body of innerFunction executes, and its returned value is evaluated.
  5. Return value is passed back to outerFunction, which in turn can return that value.
  6. Stack frames are popped off the...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Professional JavaScript for Web Developers - Fourth Edition
Published in: Nov 2019Publisher: WileyISBN-13: 9781119366447

Author (1)

author image
Matt Frisbie

Matt Frisbie has worked in web development for over a decade. During that time, he's been a startup co-founder, an engineer at a Big Four tech company, and the first engineer at a Y Combinator startup that would eventually become a billion-dollar company. As a Google software engineer, Matt worked on both the AdSense and Accelerated Mobile Pages (AMP) platforms; his code contributions run on most of the planet's web browsing devices. Prior to this, Matt was the first engineer at DoorDash, where he helped lay the foundation for a company that has become the leader in online food delivery. Matt has written two books and recorded two video series for O'Reilly and Packt, speaks at frontend meetups and web casts, and is a level 1 sommelier. He majored in Computer Engineering at the University of Illinois Urbana-Champaign. Matt's Twitter handle is @mattfriz.
Read more about Matt Frisbie