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 .