Does Babel do with tail option optimization "es2016"?

I used the following example to check tail call recursion with Babel and es2016 preset:

 'use strict'; try { function r(n) { if (n%5000===0) console.log(`reached a depth of ${n}`); r(n+1); } r(0); } catch (e) { if (!(e instanceof RangeError)) throw e; else console.log('stack blown'); } 

My package.json file:

 { "name": "tail-call-optimization", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "build": "babel es6 --out-dir es5 --source-maps", "watch": "babel es6 --out-dir es5 --source-maps --watch", "start": "node es5/app.js" }, "author": "", "license": "ISC", "devDependencies": { "babel-cli": "^6.6.5", "babel-core": "^6.7.4", "babel-loader": "^6.2.4", "babel-polyfill": "^6.7.4", "babel-preset-es2016": "^6.0.10", "babel-runtime": "^6.6.1" }, "dependencies": { "babel-polyfill": "^6.7.4", "source-map-support": "^0.4.0" } } 

... and .babelrc simple:

 { "presets": ["es2016"] } 

Run above:

 npm run build && npm run start 

... displays the following console output:

 reached a depth of 0 reached a depth of 5000 reached a depth of 10000 reached a depth of 15000 stack blown 

In fact, looking at the crowded file in the es5 directory, there is nothing to suggest that TCO was implemented.

Did I miss something?

My version is node 4.3.2 .

+5
source share
2 answers

None of the “official” Babel 6 plugins / presets currently implement TCO. babel-preset-es2016 not an official preset. If TCO does not rely on the support of the parser in Babylon (from my point of view, I would not think so, but I'm not sure), then I assume that the plugin / preset for user programs can implement it and, possibly, (but not that what I know from). Here is the problem of tracking a possible “official” re-implementation: T2614 . If someone wants a PR that links to Learn ES2015 @Marcus, write me here and I will combine it.

0
source

Looking at: https://babeljs.io/docs/learn-es2015/ , you read:

Temporarily removed in Babel 6

Only explicit tail-bound recursion was supported due to the complexity and impact of performance on tail-call support worldwide. Removed due to other errors and will be re-executed.

So, I assume that this is not implemented at the moment.

+5
source

Source: https://habr.com/ru/post/1246527/


All Articles