Message from JavaScript discussions
January 2019
— When you run loops with different context, the functional perf is not guaranteed. "normal" loop has stable perf
Optimizer may decide to inline function, dont create context for that iterator or it may decide to de-optimize
— It's guaranteed with proper tail calls
— That's irrelevant for these cases, there is no inlining going on in the loop nor in the recursive definition
— Inlining is the matter of optimizing compiler, you may make a hint but you dont control it.
— I know, but there is nothing to inline in these examples
— I'll post the code I used
—
'use strict';
let sum = 0;
console.time('loop');
for (let i = 0; i < 100000000; ++i) {
sum += i;
}
console.log(sum);
console.timeEnd('loop');
—
'use strict';
const sum = (i, acc) =>
i < 100000000
? sum(i + 1, i + acc)
: acc;
console.time('ptc');
console.log(sum(0, 0));
console.timeEnd('ptc');
— Definitly, it will be inlined
— How?
— It can't