orans-recurse-debug
v1.1.0
Published
Trace recursive functions and see the call tree and real cost
Maintainers
Readme
orans-recurse-debug
Watch recursion actually run. Point it at any recursive function and it prints the call tree plus a summary of the real cost.
Install
npm install orans-recurse-debugUsage
const { trace } = require("orans-recurse-debug");
let fib = function (n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
};
fib = trace(fib, { name: "fib" });
fib(6);The one rule: declare with let name = function (...), then reassign
name = trace(name). If you write trace(function name() {...}), the inner
recursive call reaches the original function and only the outer call is traced.
What you get
- Indented call tree, one colour per depth
- Red
repeat #ntag when the same arguments come through twice - Summary: total calls, deepest stack, how many calls redid work
Extras
note("label", value) — print a line inside the current call, correctly indented.
highlight(arr, i, j) — render an array with two positions boxed.
trace(fn, { maxDepth: 20 }) — throw instead of blowing the stack. Default 50.
