[{"data":1,"prerenderedAt":732},["ShallowReactive",2],{"authors":3,"article-2022-06-02-tail-call-optimizing-recursion":331},[4,23,35,48,61,73,85,98,111,124,136,148,161,173,185,197,209,221,233,245,258,270,282,295,307,319],{"id":5,"title":6,"body":7,"description":11,"extension":14,"meta":15,"name":16,"navigation":17,"path":18,"readingTime":19,"seo":20,"stem":21,"__hash__":22},"authors\u002Fauthors\u002Falexandre-guillon.md","Software Engineer",{"type":8,"value":9,"toc":10},"minimark",[],{"title":11,"searchDepth":12,"depth":12,"links":13},"",2,[],"md",{},"Alexandre Guillon",true,"\u002Fauthors\u002Falexandre-guillon",1,{"title":6,"description":11},"authors\u002Falexandre-guillon","4tf48mjyjFNqItOHaulICbrjeCyMag1o6801uHeTz98",{"id":24,"title":6,"body":25,"description":11,"extension":14,"meta":29,"name":30,"navigation":17,"path":31,"readingTime":19,"seo":32,"stem":33,"__hash__":34},"authors\u002Fauthors\u002Falexis-ablain.md",{"type":8,"value":26,"toc":27},[],{"title":11,"searchDepth":12,"depth":12,"links":28},[],{},"Alexis Ablain","\u002Fauthors\u002Falexis-ablain",{"title":6,"description":11},"authors\u002Falexis-ablain","_SIAtB7f-39e5t3GiJof81NP47s6MGo2n4gaHkTy1uQ",{"id":36,"title":37,"body":38,"description":11,"extension":14,"meta":42,"name":43,"navigation":17,"path":44,"readingTime":19,"seo":45,"stem":46,"__hash__":47},"authors\u002Fauthors\u002Faxel-shaita.md","Engineering Manager",{"type":8,"value":39,"toc":40},[],{"title":11,"searchDepth":12,"depth":12,"links":41},[],{},"Axel Shaïta","\u002Fauthors\u002Faxel-shaita",{"title":37,"description":11},"authors\u002Faxel-shaita","fK0argUhsBkWLjpTAhY13oYLVzQthcEYkCEdtHWmIgE",{"id":49,"title":50,"body":51,"description":11,"extension":14,"meta":55,"name":56,"navigation":17,"path":57,"readingTime":19,"seo":58,"stem":59,"__hash__":60},"authors\u002Fauthors\u002Fbaptiste-faure.md","Head of Talent Acquisition",{"type":8,"value":52,"toc":53},[],{"title":11,"searchDepth":12,"depth":12,"links":54},[],{},"Baptiste Faure","\u002Fauthors\u002Fbaptiste-faure",{"title":50,"description":11},"authors\u002Fbaptiste-faure","ELisToYtcgHmgdVWZkCclTPV6exZtfyXqhpx1jjbJHs",{"id":62,"title":6,"body":63,"description":11,"extension":14,"meta":67,"name":68,"navigation":17,"path":69,"readingTime":19,"seo":70,"stem":71,"__hash__":72},"authors\u002Fauthors\u002Fbenjamin-bouillot.md",{"type":8,"value":64,"toc":65},[],{"title":11,"searchDepth":12,"depth":12,"links":66},[],{},"Benjamin Bouillot","\u002Fauthors\u002Fbenjamin-bouillot",{"title":6,"description":11},"authors\u002Fbenjamin-bouillot","tbhCFZyfTt7ZM5b5YgqQ2nhgnSTl8BweaQQryc87fHo",{"id":74,"title":37,"body":75,"description":11,"extension":14,"meta":79,"name":80,"navigation":17,"path":81,"readingTime":19,"seo":82,"stem":83,"__hash__":84},"authors\u002Fauthors\u002Fcedric-nicoloso.md",{"type":8,"value":76,"toc":77},[],{"title":11,"searchDepth":12,"depth":12,"links":78},[],{},"Cédric Nicoloso","\u002Fauthors\u002Fcedric-nicoloso",{"title":37,"description":11},"authors\u002Fcedric-nicoloso","ibSoh4VZYiWYTuLOnZTedaAfcnvet1Q9H7ogW0LgorY",{"id":86,"title":87,"body":88,"description":11,"extension":14,"meta":92,"name":93,"navigation":17,"path":94,"readingTime":19,"seo":95,"stem":96,"__hash__":97},"authors\u002Fauthors\u002Fdavid-touzet.md","Staff Engineer",{"type":8,"value":89,"toc":90},[],{"title":11,"searchDepth":12,"depth":12,"links":91},[],{},"David Touzet","\u002Fauthors\u002Fdavid-touzet",{"title":87,"description":11},"authors\u002Fdavid-touzet","dHWwnQxb1Ubt-WwXWEODGEo9AFoq1cJUhfg3kdnYSBM",{"id":99,"title":100,"body":101,"description":11,"extension":14,"meta":105,"name":106,"navigation":17,"path":107,"readingTime":19,"seo":108,"stem":109,"__hash__":110},"authors\u002Fauthors\u002Feloise-chizat.md","Data Engineer",{"type":8,"value":102,"toc":103},[],{"title":11,"searchDepth":12,"depth":12,"links":104},[],{},"Eloïse Chizat","\u002Fauthors\u002Feloise-chizat",{"title":100,"description":11},"authors\u002Feloise-chizat","Utd72Vm9qT4hh2ZbFi6a2_nXw5Wb494Ed_HL1ra5yw8",{"id":112,"title":113,"body":114,"description":11,"extension":14,"meta":118,"name":119,"navigation":17,"path":120,"readingTime":19,"seo":121,"stem":122,"__hash__":123},"authors\u002Fauthors\u002Femmanuel-auclair.md","Staff engineer",{"type":8,"value":115,"toc":116},[],{"title":11,"searchDepth":12,"depth":12,"links":117},[],{},"Emmanuel Auclair","\u002Fauthors\u002Femmanuel-auclair",{"title":113,"description":11},"authors\u002Femmanuel-auclair","MtsA8THNLEn0dTtYEIQaGwDuf7MjQL55IOeei5gugEg",{"id":125,"title":6,"body":126,"description":11,"extension":14,"meta":130,"name":131,"navigation":17,"path":132,"readingTime":19,"seo":133,"stem":134,"__hash__":135},"authors\u002Fauthors\u002Fhoreb-parraud.md",{"type":8,"value":127,"toc":128},[],{"title":11,"searchDepth":12,"depth":12,"links":129},[],{},"Horeb Parraud","\u002Fauthors\u002Fhoreb-parraud",{"title":6,"description":11},"authors\u002Fhoreb-parraud","ajjsnUX4ohZI-ghMdbb92q_taWDkKXVZSLZXoAeLQtg",{"id":137,"title":37,"body":138,"description":11,"extension":14,"meta":142,"name":143,"navigation":17,"path":144,"readingTime":19,"seo":145,"stem":146,"__hash__":147},"authors\u002Fauthors\u002Fhugo-contreras.md",{"type":8,"value":139,"toc":140},[],{"title":11,"searchDepth":12,"depth":12,"links":141},[],{},"Hugo Contreras","\u002Fauthors\u002Fhugo-contreras",{"title":37,"description":11},"authors\u002Fhugo-contreras","2nc3VMu9ASq9Z6Pwx2-7-Ye991Pww4p-UEDBQFfjF-Q",{"id":149,"title":150,"body":151,"description":11,"extension":14,"meta":155,"name":156,"navigation":17,"path":157,"readingTime":19,"seo":158,"stem":159,"__hash__":160},"authors\u002Fauthors\u002Fjulien-tassin.md","Head of Engineering",{"type":8,"value":152,"toc":153},[],{"title":11,"searchDepth":12,"depth":12,"links":154},[],{},"Julien Tassin","\u002Fauthors\u002Fjulien-tassin",{"title":150,"description":11},"authors\u002Fjulien-tassin","iUIHI7SITje38Jh9X9uvYs4-VsHx4eCdt6hAlyLFG_o",{"id":162,"title":6,"body":163,"description":11,"extension":14,"meta":167,"name":168,"navigation":17,"path":169,"readingTime":19,"seo":170,"stem":171,"__hash__":172},"authors\u002Fauthors\u002Flaurent-renard.md",{"type":8,"value":164,"toc":165},[],{"title":11,"searchDepth":12,"depth":12,"links":166},[],{},"Laurent Renard","\u002Fauthors\u002Flaurent-renard",{"title":6,"description":11},"authors\u002Flaurent-renard","5BP7Ed-pt1SQHjh0UJ1XUrlLTcdlFaDoKBCP4deHq8A",{"id":174,"title":6,"body":175,"description":11,"extension":14,"meta":179,"name":180,"navigation":17,"path":181,"readingTime":19,"seo":182,"stem":183,"__hash__":184},"authors\u002Fauthors\u002Fleo-martin.md",{"type":8,"value":176,"toc":177},[],{"title":11,"searchDepth":12,"depth":12,"links":178},[],{},"Léo Martin","\u002Fauthors\u002Fleo-martin",{"title":6,"description":11},"authors\u002Fleo-martin","eYxCHkRgbGDV7shKdTA9s7Tu0zGV4yDGFoKR5MHQntY",{"id":186,"title":6,"body":187,"description":11,"extension":14,"meta":191,"name":192,"navigation":17,"path":193,"readingTime":19,"seo":194,"stem":195,"__hash__":196},"authors\u002Fauthors\u002Floic-bousquet.md",{"type":8,"value":188,"toc":189},[],{"title":11,"searchDepth":12,"depth":12,"links":190},[],{},"Loïc Bousquet","\u002Fauthors\u002Floic-bousquet",{"title":6,"description":11},"authors\u002Floic-bousquet","ko12qZwiGL8XNjAoy9oWypPkIjr29Pbq7vhdtgldqeQ",{"id":198,"title":6,"body":199,"description":11,"extension":14,"meta":203,"name":204,"navigation":17,"path":205,"readingTime":19,"seo":206,"stem":207,"__hash__":208},"authors\u002Fauthors\u002Floic-poullain.md",{"type":8,"value":200,"toc":201},[],{"title":11,"searchDepth":12,"depth":12,"links":202},[],{},"Loïc Poullain","\u002Fauthors\u002Floic-poullain",{"title":6,"description":11},"authors\u002Floic-poullain","oRIyJhFRTqxy5dLCYQ2OnYZ1DB-gLDUM-85vTSYuTF0",{"id":210,"title":100,"body":211,"description":11,"extension":14,"meta":215,"name":216,"navigation":17,"path":217,"readingTime":19,"seo":218,"stem":219,"__hash__":220},"authors\u002Fauthors\u002Fmaud-lelu.md",{"type":8,"value":212,"toc":213},[],{"title":11,"searchDepth":12,"depth":12,"links":214},[],{},"Maud Lélu","\u002Fauthors\u002Fmaud-lelu",{"title":100,"description":11},"authors\u002Fmaud-lelu","MMbsCKuE41OMHusrl12FIEsI-Trx7l8Nn_ANhvj2_y4",{"id":222,"title":37,"body":223,"description":11,"extension":14,"meta":227,"name":228,"navigation":17,"path":229,"readingTime":19,"seo":230,"stem":231,"__hash__":232},"authors\u002Fauthors\u002Fnicolas-poirier.md",{"type":8,"value":224,"toc":225},[],{"title":11,"searchDepth":12,"depth":12,"links":226},[],{},"Nicolas Poirier","\u002Fauthors\u002Fnicolas-poirier",{"title":37,"description":11},"authors\u002Fnicolas-poirier","dXrJkYo8az4SN_D23aYc3fQ7z8s1dR2a0lt1ogjAjJs",{"id":234,"title":37,"body":235,"description":11,"extension":14,"meta":239,"name":240,"navigation":17,"path":241,"readingTime":19,"seo":242,"stem":243,"__hash__":244},"authors\u002Fauthors\u002Fraphael-sauget.md",{"type":8,"value":236,"toc":237},[],{"title":11,"searchDepth":12,"depth":12,"links":238},[],{},"Raphaël Sauget","\u002Fauthors\u002Fraphael-sauget",{"title":37,"description":11},"authors\u002Fraphael-sauget","Uri9bcq0QDuxRA0PbBoNtu7p_5L3dALu4kzcXVW0xyM",{"id":246,"title":247,"body":248,"description":11,"extension":14,"meta":252,"name":253,"navigation":17,"path":254,"readingTime":19,"seo":255,"stem":256,"__hash__":257},"authors\u002Fauthors\u002Fromain-koenig.md","Co-funder & Head of innovation",{"type":8,"value":249,"toc":250},[],{"title":11,"searchDepth":12,"depth":12,"links":251},[],{},"Romain Koenig","\u002Fauthors\u002Fromain-koenig",{"title":247,"description":11},"authors\u002Fromain-koenig","uyS8--eG2_ezyqRABcJnMJmQKKuSArhPWd14aUvFeEw",{"id":259,"title":37,"body":260,"description":11,"extension":14,"meta":264,"name":265,"navigation":17,"path":266,"readingTime":19,"seo":267,"stem":268,"__hash__":269},"authors\u002Fauthors\u002Fromaric-juniet.md",{"type":8,"value":261,"toc":262},[],{"title":11,"searchDepth":12,"depth":12,"links":263},[],{},"Romaric Juniet","\u002Fauthors\u002Fromaric-juniet",{"title":37,"description":11},"authors\u002Fromaric-juniet","4Zb2artgT-eo-PHLXi3xi4d5t7s6PfhUxeSfXIikSUY",{"id":271,"title":6,"body":272,"description":11,"extension":14,"meta":276,"name":277,"navigation":17,"path":278,"readingTime":19,"seo":279,"stem":280,"__hash__":281},"authors\u002Fauthors\u002Fstanyslas-bres.md",{"type":8,"value":273,"toc":274},[],{"title":11,"searchDepth":12,"depth":12,"links":275},[],{},"Stanyslas Bres","\u002Fauthors\u002Fstanyslas-bres",{"title":6,"description":11},"authors\u002Fstanyslas-bres","Xa0SahETuiN4q1jrmR2ych3moAqcZ2LbU7vSfEt2RuU",{"id":283,"title":284,"body":285,"description":11,"extension":14,"meta":289,"name":290,"navigation":17,"path":291,"readingTime":19,"seo":292,"stem":293,"__hash__":294},"authors\u002Fauthors\u002Ftalent-acquisition.md","Talent Acquisition",{"type":8,"value":286,"toc":287},[],{"title":11,"searchDepth":12,"depth":12,"links":288},[],{},"Équipe Talent Acquisition","\u002Fauthors\u002Ftalent-acquisition",{"description":11},"authors\u002Ftalent-acquisition","doDfE76txftQ4wIiKjJoDmSpyzSKk0tzlgVAp6-opAY",{"id":296,"title":6,"body":297,"description":11,"extension":14,"meta":301,"name":302,"navigation":17,"path":303,"readingTime":19,"seo":304,"stem":305,"__hash__":306},"authors\u002Fauthors\u002Fvictor-borg.md",{"type":8,"value":298,"toc":299},[],{"title":11,"searchDepth":12,"depth":12,"links":300},[],{},"Victor Borg","\u002Fauthors\u002Fvictor-borg",{"title":6,"description":11},"authors\u002Fvictor-borg","-Za-JweoiP6hyclue_WkxMXdRUDTczPGlJf6AZckjUc",{"id":308,"title":6,"body":309,"description":11,"extension":14,"meta":313,"name":314,"navigation":17,"path":315,"readingTime":19,"seo":316,"stem":317,"__hash__":318},"authors\u002Fauthors\u002Fvirgil-roger.md",{"type":8,"value":310,"toc":311},[],{"title":11,"searchDepth":12,"depth":12,"links":312},[],{},"Virgil Roger","\u002Fauthors\u002Fvirgil-roger",{"title":6,"description":11},"authors\u002Fvirgil-roger","DfVFe5j0bCgXeEr381ZYOM5DP4m-pWb93J9-m_muKJ0",{"id":320,"title":6,"body":321,"description":11,"extension":14,"meta":325,"name":326,"navigation":17,"path":327,"readingTime":19,"seo":328,"stem":329,"__hash__":330},"authors\u002Fauthors\u002Fyukan-zhao.md",{"type":8,"value":322,"toc":323},[],{"title":11,"searchDepth":12,"depth":12,"links":324},[],{},"Yukan Zhao","\u002Fauthors\u002Fyukan-zhao",{"title":6,"description":11},"authors\u002Fyukan-zhao","LRPHugtAJnWHsmHxy9_SR5Zas_C5p-GR_uHEs1Fhk_E",{"id":332,"title":333,"author":334,"body":335,"date":722,"description":723,"extension":14,"lang":724,"meta":725,"navigation":17,"path":726,"published":17,"readingTime":425,"seo":727,"stem":728,"tags":729,"__hash__":731},"articles\u002Farticles\u002F2022-06-02-tail-call-optimizing-recursion.md","Tail call: Optimizing recursion","nicolas-poirier",{"type":8,"value":336,"toc":718},[337,350,353,381,386,513,516,522,531,538,547,553,557,560,563,566,675,677,682,698,701,706,711,714],[338,339,340,341,345,346,349],"p",{},"Recursive functions are recommended to solve some problems, or imposed by languages like pure\nfunctional programming languages which don’t have ",[342,343,344],"code",{},"for"," and ",[342,347,348],{},"while"," loops. But recursion may comes\nat a price.",[338,351,352],{},"Let’s use recursion to sum the elements of an array and understand how to optimize it with tail call\noptimization.",[338,354,355,356,363,364,363,369,374,375,380],{},"Note that I use JavaScript and although tail call optimization appeared in\n",[357,358,362],"a",{"href":359,"rel":360},"https:\u002F\u002F262.ecma-international.org\u002F6.0\u002F#sec-tail-position-calls",[361],"nofollow","ES 6 specifications",",\n",[357,365,368],{"href":366,"rel":367},"https:\u002F\u002Fkangax.github.io\u002Fcompat-table\u002Fes6\u002F",[361],"most engines don’t implement it",[357,370,373],{"href":371,"rel":372},"https:\u002F\u002Fv8.dev\u002Fblog\u002Fmodern-javascript#proper-tail-calls",[361],"including V8",". Apple WebKit is one of the\nonly ones where it is available. Other languages implementing tail call optimization are listed in\nthe ",[357,376,379],{"href":377,"rel":378},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FTail_call#Language_support",[361],"Wikipedia article about tail call",".",[382,383,385],"h2",{"id":384},"naive-implementation","Naive implementation",[387,388,392],"pre",{"className":389,"code":390,"language":391,"meta":11,"style":11},"language-ts shiki shiki-themes github-light github-dark","function sum(x) {\n  \u002F\u002F Base case. The sum of no elements is 0\n  if (x.length === 0) {\n    return 0;\n  }\n\n  \u002F\u002F Add the first element to the sum of all the other elements\n  return x[0] + sum(x.slice(1));\n}\n","ts",[342,393,394,417,423,444,455,461,467,473,507],{"__ignoreMap":11},[395,396,398,402,406,410,414],"span",{"class":397,"line":19},"line",[395,399,401],{"class":400},"szBVR","function",[395,403,405],{"class":404},"sScJk"," sum",[395,407,409],{"class":408},"sVt8B","(",[395,411,413],{"class":412},"s4XuR","x",[395,415,416],{"class":408},") {\n",[395,418,419],{"class":397,"line":12},[395,420,422],{"class":421},"sJ8bj","  \u002F\u002F Base case. The sum of no elements is 0\n",[395,424,426,429,432,436,439,442],{"class":397,"line":425},3,[395,427,428],{"class":400},"  if",[395,430,431],{"class":408}," (x.",[395,433,435],{"class":434},"sj4cs","length",[395,437,438],{"class":400}," ===",[395,440,441],{"class":434}," 0",[395,443,416],{"class":408},[395,445,447,450,452],{"class":397,"line":446},4,[395,448,449],{"class":400},"    return",[395,451,441],{"class":434},[395,453,454],{"class":408},";\n",[395,456,458],{"class":397,"line":457},5,[395,459,460],{"class":408},"  }\n",[395,462,464],{"class":397,"line":463},6,[395,465,466],{"emptyLinePlaceholder":17},"\n",[395,468,470],{"class":397,"line":469},7,[395,471,472],{"class":421},"  \u002F\u002F Add the first element to the sum of all the other elements\n",[395,474,476,479,482,485,488,491,493,496,499,501,504],{"class":397,"line":475},8,[395,477,478],{"class":400},"  return",[395,480,481],{"class":408}," x[",[395,483,484],{"class":434},"0",[395,486,487],{"class":408},"] ",[395,489,490],{"class":400},"+",[395,492,405],{"class":404},[395,494,495],{"class":408},"(x.",[395,497,498],{"class":404},"slice",[395,500,409],{"class":408},[395,502,503],{"class":434},"1",[395,505,506],{"class":408},"));\n",[395,508,510],{"class":397,"line":509},9,[395,511,512],{"class":408},"}\n",[338,514,515],{},"It runs like this:",[338,517,518],{},[519,520],"img",{"alt":11,"src":521},"\u002Fimages\u002Fimage-1-1.webp",[338,523,524,525,530],{},"Comes ",[357,526,529],{"href":527,"rel":528},"https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FGlossary\u002FCall_stack",[361],"the call stack",". The call stack\nkeeps track of where the interpreter is in a program that calls multiple functions. When the program\ncalls a function, the interpreter adds that function on the top of the call stack. When the function\nis finished, the interpreter takes it off the stack.",[338,532,533,534,537],{},"Basically that’s how it computes ",[342,535,536],{},"sum([1, 2, 3])",":",[338,539,541,542],{"align":540},"center","\n  ",[519,543],{"src":544,"alt":545,"style":546},"\u002Fimages\u002Fcall-stack-no-tail-call-1-_2_.jpg","Call Stack Example","width:150px; object-fit:contain;",[338,548,549,552],{},[342,550,551],{},"[]"," is the base case. It returns 0. The interpreter has stacked 4 frames. Now it can start\nunstacking. With bigger arrays (about 10 000 elements depending on configuration), the call stack\ncould reach its maximum size, crashing the program.",[382,554,556],{"id":555},"tail-call-to-the-rescue","Tail call to the rescue",[338,558,559],{},"Tail call means that the final action of a function is a function call.",[338,561,562],{},"In the previous implementation, the final action of the function was an addition.",[338,564,565],{},"Let’s apply tail call. To do that, you have to add an accumulator argument to the function to save\nthe partial sum. Using an accumulator is often required to apply tail call.",[387,567,569],{"className":389,"code":568,"language":391,"meta":11,"style":11},"function sum(x, acc = 0) {\n  \u002F\u002F Base case: no more elements, returns the accumulator\n  if (x.length === 0) {\n    return acc;\n  }\n\n  \u002F\u002F Call sum() without the first element\n  \u002F\u002F Add the first element to the accumulator\n  \u002F\u002F The recursive call to sum() is now the final action of the function\n  return sum(x.slice(1), acc + x[0]);\n}\n",[342,570,571,594,599,613,620,624,628,633,638,643,670],{"__ignoreMap":11},[395,572,573,575,577,579,581,584,587,590,592],{"class":397,"line":19},[395,574,401],{"class":400},[395,576,405],{"class":404},[395,578,409],{"class":408},[395,580,413],{"class":412},[395,582,583],{"class":408},", ",[395,585,586],{"class":412},"acc",[395,588,589],{"class":400}," =",[395,591,441],{"class":434},[395,593,416],{"class":408},[395,595,596],{"class":397,"line":12},[395,597,598],{"class":421},"  \u002F\u002F Base case: no more elements, returns the accumulator\n",[395,600,601,603,605,607,609,611],{"class":397,"line":425},[395,602,428],{"class":400},[395,604,431],{"class":408},[395,606,435],{"class":434},[395,608,438],{"class":400},[395,610,441],{"class":434},[395,612,416],{"class":408},[395,614,615,617],{"class":397,"line":446},[395,616,449],{"class":400},[395,618,619],{"class":408}," acc;\n",[395,621,622],{"class":397,"line":457},[395,623,460],{"class":408},[395,625,626],{"class":397,"line":463},[395,627,466],{"emptyLinePlaceholder":17},[395,629,630],{"class":397,"line":469},[395,631,632],{"class":421},"  \u002F\u002F Call sum() without the first element\n",[395,634,635],{"class":397,"line":475},[395,636,637],{"class":421},"  \u002F\u002F Add the first element to the accumulator\n",[395,639,640],{"class":397,"line":509},[395,641,642],{"class":421},"  \u002F\u002F The recursive call to sum() is now the final action of the function\n",[395,644,646,648,650,652,654,656,658,661,663,665,667],{"class":397,"line":645},10,[395,647,478],{"class":400},[395,649,405],{"class":404},[395,651,495],{"class":408},[395,653,498],{"class":404},[395,655,409],{"class":408},[395,657,503],{"class":434},[395,659,660],{"class":408},"), acc ",[395,662,490],{"class":400},[395,664,481],{"class":408},[395,666,484],{"class":434},[395,668,669],{"class":408},"]);\n",[395,671,673],{"class":397,"line":672},11,[395,674,512],{"class":408},[338,676,515],{},[338,678,679],{},[519,680],{"alt":11,"src":681},"\u002Fimages\u002Fimage-1.png",[338,683,684,685,688,689,692,693,695,696,380],{},"The interpreter doesn’t have to go back to the previous ",[342,686,687],{},"sum()"," call. It only has to go back to the\noriginal caller ",[342,690,691],{},"sumCaller()",". Thereby, the call stack doesn’t need to keep track of all calls to\n",[342,694,687],{},". That allows an optimization called tail call optimization. Interpreters that implement tail\ncall optimization will only use one frame to keep track of the current call to ",[342,697,687],{},[338,699,700],{},"With tail call optimization, the call stack is like this:",[338,702,541,703],{"align":540},[519,704],{"src":705,"alt":545,"style":546},"\u002Fimages\u002Fcall-stack-tail-call-1.png",[338,707,708,709,380],{},"Now, even with bigger arrays, the call stack will only use one frame to compute ",[342,710,687],{},[338,712,713],{},"Be careful using recursion and think about tail call optimization.",[715,716,717],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":11,"searchDepth":12,"depth":12,"links":719},[720,721],{"id":384,"depth":12,"text":385},{"id":555,"depth":12,"text":556},"2022-06-02","Let’s use recursion to sum the elements of an array and understand how to optimize it with tail call optimization.","en",{},"\u002Farticles\u002F2022-06-02-tail-call-optimizing-recursion",{"title":333,"description":723},"articles\u002F2022-06-02-tail-call-optimizing-recursion",[730],"Tech","vmFzQVTjF9PAQgSx1HNX5gB2xfudOQXPaQqbUvuxPx0",1778159243895]