{"action":"created","issue":{"url":"https://api.github.com/repos/6to5/6to5/issues/256","labels_url":"https://api.github.com/repos/6to5/6to5/issues/256/labels{/name}","comments_url":"https://api.github.com/repos/6to5/6to5/issues/256/comments","events_url":"https://api.github.com/repos/6to5/6to5/issues/256/events","html_url":"https://github.com/6to5/6to5/issues/256","id":51185699,"number":256,"title":"Support for tail call optimization","user":{"login":"kobezzza","id":1149243,"avatar_url":"https://avatars.githubusercontent.com/u/1149243?v=3","gravatar_id":"","url":"https://api.github.com/users/kobezzza","html_url":"https://github.com/kobezzza","followers_url":"https://api.github.com/users/kobezzza/followers","following_url":"https://api.github.com/users/kobezzza/following{/other_user}","gists_url":"https://api.github.com/users/kobezzza/gists{/gist_id}","starred_url":"https://api.github.com/users/kobezzza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kobezzza/subscriptions","organizations_url":"https://api.github.com/users/kobezzza/orgs","repos_url":"https://api.github.com/users/kobezzza/repos","events_url":"https://api.github.com/users/kobezzza/events{/privacy}","received_events_url":"https://api.github.com/users/kobezzza/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/6to5/6to5/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2014-12-06T15:09:31Z","updated_at":"2015-01-13T08:28:38Z","closed_at":null,"body":"http://www.2ality.com/2014/04/call-stack-size.html\r\n*ECMAScript 6 will have tail call optimization: If a function call is the last action in a function, it is handled via a “jump”, not via a “subroutine call”. That means that, if you slightly rewrote computeMaxCallStackSize(), it would run forever under ECMAScript 6 (in strict mode)*\r\n\r\n**Tail call recursion**\r\n```js\r\nfunction fac_times(n, acc) {\r\n\treturn (n == 0) ? acc : fac_times(n - 1, acc * n);\r\n}\r\n\r\nfunction factorial(n) {\r\n\treturn fac_times(n, 1);\r\n}\r\n```\r\n\r\n**After optimization**\r\n```js\r\nfunction fac_times(n, acc) {\r\n\twhile (true) {\r\n\t\tif (n == 0) {\r\n\t\t\treturn acc;\r\n\t\t}\r\n\t\r\n\t\tvar old_n = n,\r\n\t\t\told_acc = acc;\r\n\t\r\n\t\tn = old_n - 1;\r\n\t\tacc = old_acc * old_n;\r\n\t}\r\n}\r\n\r\nfunction factorial(n) {\r\n\treturn fac_times(n, 1);\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/6to5/6to5/issues/comments/69710609","html_url":"https://github.com/6to5/6to5/issues/256#issuecomment-69710609","issue_url":"https://api.github.com/repos/6to5/6to5/issues/256","id":69710609,"user":{"login":"impinball","id":4483844,"avatar_url":"https://avatars.githubusercontent.com/u/4483844?v=3","gravatar_id":"","url":"https://api.github.com/users/impinball","html_url":"https://github.com/impinball","followers_url":"https://api.github.com/users/impinball/followers","following_url":"https://api.github.com/users/impinball/following{/other_user}","gists_url":"https://api.github.com/users/impinball/gists{/gist_id}","starred_url":"https://api.github.com/users/impinball/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/impinball/subscriptions","organizations_url":"https://api.github.com/users/impinball/orgs","repos_url":"https://api.github.com/users/impinball/repos","events_url":"https://api.github.com/users/impinball/events{/privacy}","received_events_url":"https://api.github.com/users/impinball/received_events","type":"User","site_admin":false},"created_at":"2015-01-13T08:28:38Z","updated_at":"2015-01-13T08:28:38Z","body":"@all Here's another good candidate of tail call elimination. AFAICT, it is a tail call according to the draft.\r\n\r\n```js\r\nfunction reduce(xs, f) {\r\n  switch (xs.length) {\r\n  case 0: return null;\r\n  case 1: return xs[0];\r\n  case 2: return f(xs[0], xs[1]);\r\n  default:\r\n    let [first, second, ...rest] = xs;\r\n    return reduceRight([f(first, second), ...rest], f);\r\n  }\r\n}\r\n```"}}