{"id":15408283,"url":"https://github.com/krzkaczor/babel-plugin-tailcall-optimization","last_synced_at":"2025-04-06T12:10:51.658Z","repository":{"id":66105962,"uuid":"65335671","full_name":"krzkaczor/babel-plugin-tailcall-optimization","owner":"krzkaczor","description":"Tail call optimization for JavaScript!","archived":false,"fork":false,"pushed_at":"2022-01-09T23:59:33.000Z","size":78,"stargazers_count":193,"open_issues_count":2,"forks_count":9,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-02-23T04:26:08.515Z","etag":null,"topics":["babel","javascript","tail-call-optimization"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krzkaczor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-09T23:45:12.000Z","updated_at":"2025-01-21T19:12:34.000Z","dependencies_parsed_at":"2023-02-21T23:15:21.545Z","dependency_job_id":null,"html_url":"https://github.com/krzkaczor/babel-plugin-tailcall-optimization","commit_stats":{"total_commits":38,"total_committers":8,"mean_commits":4.75,"dds":0.3421052631578947,"last_synced_commit":"91c09c722488b97cb5c518a934c906769fea52d7"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krzkaczor%2Fbabel-plugin-tailcall-optimization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krzkaczor%2Fbabel-plugin-tailcall-optimization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krzkaczor%2Fbabel-plugin-tailcall-optimization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krzkaczor%2Fbabel-plugin-tailcall-optimization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krzkaczor","download_url":"https://codeload.github.com/krzkaczor/babel-plugin-tailcall-optimization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266487,"owners_count":20910833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel","javascript","tail-call-optimization"],"created_at":"2024-10-01T16:33:13.625Z","updated_at":"2025-04-06T12:10:51.629Z","avatar_url":"https://github.com/krzkaczor.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# babel-plugin-tailcall-optimization\n[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\nTail call optimization for JavaScript!\n\n## Installation\n\n```bash\nnpm install babel-plugin-tailcall-optimization --save-dev\n```\n\nand add to your `.babelrc`:\n\n```js\n\"plugins\": [\"tailcall-optimization\"]\n```\n\n*if you use babel@6 use `babel-plugin-tailcall-optimization@1` package*\n\n\n## How does it work?\nWe rewrite functions with tail calls to ones using while loops. Original function with tail call:\n```js\nfunction counter (n, acc = 0) {\n  if (n === 0) {\n    return acc\n  } else {\n    return counter(n - 1, acc + 1)\n  }\n}\n```\n\ngets rewritten to this:\n```js\nfunction counter(n, acc = 0) {\n  var _repeat = true;\n\n  var _n, _acc;\n\n  while (_repeat) {\n    _repeat = false;\n\n    if (n === 0) {\n      return acc;\n    } else {\n      _n = n - 1\n      _acc = acc + 1\n      n = _n\n      acc = _acc\n      _repeat = true;\n      continue;\n    }\n  }\n}\n```\nPlugin does not affect functions without TCOs so it's safe to use.\n\n## Benchmarks\nFor [Fibonacci Sequence example](https://github.com/krzkaczor/babel-plugin-tailcall-optimization/blob/master/examples/fibonacciSeq.js) benchmark.js results are:\n\n```\nFibonacci Sequence without TCO x 270,170 ops/sec ±1.14% (85 runs sampled)\nFibonacci Sequence with TCO x 1,298,276 ops/sec ±1.24% (83 runs sampled)\n```\n\nSo function after TCO optimization is almost **5 times faster**.\n\n[Benchmark code](https://github.com/krzkaczor/babel-plugin-tailcall-optimization/blob/master/benchmark/fibonacciSeq.js)\n \n## Known issues\n - Currently when plugin detects function creation within tailcalled function it does not optimize it. It's related to difficulties in implementation (function scoping rules). Read more: https://phabricator.babeljs.io/T6869\n\n - It does not work for mutual recursive functions. I guess it's not super big problem - even JVM does not do this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrzkaczor%2Fbabel-plugin-tailcall-optimization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrzkaczor%2Fbabel-plugin-tailcall-optimization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrzkaczor%2Fbabel-plugin-tailcall-optimization/lists"}