Here’s a function that will work in all major browsers, although it won’t work in ECMAScript 5 strict mode because arguments.callee
and caller
have been removed in strict mode.
function getCallStackSize() {
var count = 0, fn = arguments.callee;
while ( (fn = fn.caller) ) {
count++;
}
return count;
}
Example:
function f() { g(); }
function g() { h(); }
function h() { alert(getCallStackSize()); }
f(); // Alerts 3
UPDATE 1 November 2011
In ES5 strict mode, there is simply no way to navigate the call stack. The only option left is to parse the string returned by new Error().stack
, which is non-standard, not universally supported and obviously problematic, and even this may not be possible for ever.
UPDATE 13 August 2013
This method is also limited by the fact that a function that is called more than once in a single call stack (e.g. via recursion) will throw getCallStackSize()
into an infinite loop (as pointed out by @Randomblue in the comments). An improved version of getCallStackSize()
is below: it keeps track of functions it has seen before to avoid going into an infinite loop. However, the returned value is the number of different function objects in the callstack before encountering a repeat rather than the true size of the complete call stack. This is the best you can do, unfortunately.
var arrayContains = Array.prototype.indexOf ?
function(arr, val) {
return arr.indexOf(val) > -1;
} :
function(arr, val) {
for (var i = 0, len = arr.length; i < len; ++i) {
if (arr[i] === val) {
return true;
}
}
return false;
};
function getCallStackSize() {
var count = 0, fn = arguments.callee, functionsSeen = [fn];
while ( (fn = fn.caller) && !arrayContains(functionsSeen, fn) ) {
functionsSeen.push(fn);
count++;
}
return count;
}