JS: Speed Comparison of Increment
Speed Comparison of n = n + 1 vs n++
/* bench.js 2022-07-10, 2022-08-27 comparing speed of n = n + 1 vs n++ */ const f1 = ((x) => { let n = 1; for (let i = 1; i < x; i++) n = n + 1; return n; }); const f2 = ((x) => { let n = 1; for (let i = 1; i < x; i++) n++; return n; }); console.log(f1(10) === 10); console.log(f2(10) === 10); Deno.bench("normal assign", () => { f1(10_000); }); Deno.bench("plus plus", () => { f2(10_000); });
this means,
n = n + 1
and
n++
have the same speed.