detect TCO

by Csaba Hellinger

HTML

<h1>Browser TCO detection</h1>
Error.stack is not standard, but it has basic support in all modern browsers. It has different values in different browsers, nonetheless, when a longer named function tail-calls a shorter named one, the stack trace:
<ul>
<li>with TCO: gets shorter, because the stack frame of the outer function gets replaced by the stack frame of the inner function.</li>
<li>without TCO: gets longer, because the stack frame of the inner function gets appended to the stack</li>
</ul>

Result: 
<div id="result"></div>

CSS

#result {
    color: blue;
    font-weight: bold;
}

JavaScript

"use strict";

function detectTCO() {
	const outerStackLen = new Error().stack.length;
    // name of the inner function mustn't be longer than the outer!
    return (function inner() {
    	const innerStackLen = new Error().stack.length;
    	return innerStackLen <= outerStackLen;
    }());
}

document.getElementById('result').innerText = detectTCO() ? 'TCO is available' : 'TCO is not available';