function range(s, e, res) { if (!res) { res = []; } res.push(s); if (s == e ) { return res; } else { if (s= 3) { // gather function information fn.name = fsmatch[1]; fn.args = fsmatch[2].split(/\s*,\s*/); fn.arity = fn.args.length; fn.body = fs.substring(fs.indexOf('{') + 1, fs.lastIndexOf('}')); // find occurences of self calls, replace with loop continuation recurRx = new RegExp('((^\\s*)return\\s*'+fn.name.trim()+'\\s*\\([^)]*\\);?)', 'mg'); modfn = fn.body.replace(recurRx, '$2_cont = true;\n$2continue _recur;'); // Build the new, optimized function _fbody = [ 'var _cont = true;', '_recur: while (_cont) {', ' _cont = false;', modfn, '}'].join('\n'); fn.args.unshift(null); _f = (new (Function.prototype.bind.apply(Function, fn.args.concat(_fbody)))); return _f; } else { throw new Error("Unable to transpile function: ", f.toString()); } } var _tco = tco(range); console.log(_tco.toString()); _tco(1,32768);