const str = "The quick brown fox jumps over the lazy dog.\n".repeat(100) function fold_parts(str, sep, state, fun) { for (const part of str.split(sep)) { state = fun(state, part) } return state } let sum = 0 for(let i = 0; i < 1000000; ++i) { sum += fold_parts(str, '\n', 0, (acc, line) => acc + line.length) } console.log(sum)