<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu" -->

---
title: Tail Call Optimization ♻️and Recursion🔁 in JavaScript
description: Recursion is a technique where a function calls itself to solve problems but can lead to high memory usage and stack overflow. Tail call optimization (TCO)...
canonical: https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Tail Call Optimization ♻️and Recursion🔁 in JavaScript | daily.dev
og:description: Recursion is a technique where a function calls itself to solve problems but can lead to high memory usage and stack overflow. Tail call optimization (TCO)...
og:url: https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu
og:image: https://api.daily.dev/og/posts/E6gvgvSGu.png
og:image:alt: Tail Call Optimization ♻️and Recursion🔁 in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Tail Call Optimization ♻️and Recursion🔁 in JavaScript

**[Community Picks](https://daily.dev/sources/community)** · 5 min read · 79 upvotes · 2 comments

## Summary

Recursion is a technique where a function calls itself to solve problems but can lead to high memory usage and stack overflow. Tail call optimization (TCO) addresses this by reusing stack frames for the subsequent calls when the recursive call is in the tail position. JavaScript provides partial support for TCO, with ECMAScript 6 introducing proper tail calls (PTC). Techniques like iteration, trampolining, and memoization further help optimize recursive functions. Challenges include handling deep recursion and asynchronous operations. Functional programming libraries such as Ramda offer TCO-optimized functions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://bootcamp.uxdesign.cc/tail-call-optimization-%EF%B8%8Fand-recursion-in-javascript-8768f2f06a08>

## Community discussion

Top comments from developers on daily.dev.

**@atharvashukla** · 0 upvotes

> The chrome v8 team needs to push harder for tail call elimination.
>
> A "realm" is basically an isolated execution context for JS — like web workers, iFrames, VM contexts (in Nodes.j).  There are problems with implementing PTC (Proper Tail Calls) across different realms/contexts.
>
> The implementation will require changes to the ECMAScript spec, not sure if the committee will ever reach a consensus on this though.

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#functional-programming](https://daily.dev/tags/functional-programming)

[View this post on daily.dev](https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Tail Call Optimization ♻️and Recursion🔁 in JavaScript","url":"https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu"},"datePublished":"2024-07-03T17:16:47.752Z","dateModified":"2024-10-05T17:16:32.878Z","description":"Recursion is a technique where a function calls itself to solve problems but can lead to high memory usage and stack overflow. Tail call optimization (TCO)...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a2df15f01438a522b95be727d3d0e5c5?_a=AQAEuiZ","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/a2df15f01438a522b95be727d3d0e5c5?_a=AQAEuiZ","isAccessibleForFree":true,"articleSection":"Community Picks","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Community Picks","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1655817725/logos/community","url":"https://daily.dev/sources/community"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":79},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"javascript,functional-programming","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Community Picks","item":"https://daily.dev/sources/community"},{"@type":"ListItem","position":3,"name":"Tail Call Optimization ♻️and Recursion🔁 in JavaScript"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/tail-call-optimization-and-recursion-in-javascript-e6gvgvsgu","comment":[{"@type":"Comment","text":"The chrome v8 team needs to push harder for tail call elimination.\nA “realm” is basically an isolated execution context for JS — like web workers, iFrames, VM contexts (in Nodes.j).  There are problems with implementing PTC (Proper Tail Calls) across different realms/contexts.\nThe implementation will require changes to the ECMAScript spec, not sure if the committee will ever reach a consensus on this though.","datePublished":"2024-07-09T09:14:36.718Z","url":"https://daily.dev/posts/E6gvgvSGu#c-Fg88nNld8","author":{"@type":"Person","name":"Atharva","url":"https://daily.dev/atharvashukla","image":"https://avatars.githubusercontent.com/u/33644308?v=4"}}]}
```

