Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Created August 10, 2023 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postspectacular/b06b90ce983b24c97535096b0ce1aba9 to your computer and use it in GitHub Desktop.
Save postspectacular/b06b90ce983b24c97535096b0ce1aba9 to your computer and use it in GitHub Desktop.
Performance comparison between different for-loop variations and payload sizes
import { FORMAT_MD, suite } from "@thi.ng/bench";
import { range } from "@thi.ng/transducers";
const testForIndexFwd = (src: number[]) => {
let m = 0;
for (let i = 0, n = src.length; i < n; i++) m += src[i];
return m;
};
const testForIndexRev = (src: number[]) => {
let m = 0;
for (let i = src.length; i-- > 0; ) m += src[i];
return m;
};
const testForOf = (src: number[]) => {
let m = 0;
for (let x of src) m += x;
return m;
};
const testForIter = (src: number[]) => {
let m = 0;
let x: IteratorResult<number>;
const iter = src[Symbol.iterator]();
while (!(x = iter.next()).done) m += x.value;
return m;
};
const testForEach = (src: number[]) => {
let m = 0;
src.forEach((x) => (m += x));
return m;
};
for (let [n, size] of [
[1e1, 1e5],
[1e2, 1e4],
[1e3, 1e3],
[1e4, 1e2],
[1e5, 10],
[1e6, 10],
]) {
const src = [...range(n)];
const nf = n.toExponential(0);
suite(
[
{ title: `for-fwd (${nf})`, fn: () => testForIndexFwd(src) },
{ title: `for-rev (${nf})`, fn: () => testForIndexRev(src) },
{ title: `for-of (${nf})`, fn: () => testForOf(src) },
{ title: `for-iter (${nf})`, fn: () => testForIter(src) },
{ title: `for-each (${nf})`, fn: () => testForEach(src) },
],
{
size,
iter: 1e3,
warmup: 1e3,
format: FORMAT_MD,
}
);
console.log("\n");
}

The for-each version is consistently the worst performing option (between ~1.5x - 5.7x(!) slower than a classic for-loop)

Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+1) 1000 100000 771.13 0.77 0.77 0.76 0.92 0.76 0.78 1.29
for-rev (1e+1) 1000 100000 1259.20 1.26 1.26 1.24 1.45 1.25 1.27 1.13
for-of (1e+1) 1000 100000 1147.50 1.15 1.15 1.12 1.35 1.15 1.15 0.94
for-iter (1e+1) 1000 100000 1395.20 1.40 1.40 1.37 2.08 1.38 1.40 2.85
for-each (1e+1) 1000 100000 1976.61 1.98 1.96 1.91 2.48 1.94 1.99 2.94
Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+2) 1000 10000 692.66 0.69 0.69 0.69 0.80 0.69 0.69 1.04
for-rev (1e+2) 1000 10000 1100.71 1.10 1.09 1.06 1.54 1.08 1.11 2.83
for-of (1e+2) 1000 10000 938.84 0.94 0.94 0.92 0.98 0.93 0.94 0.88
for-iter (1e+2) 1000 10000 1073.34 1.07 1.07 1.05 1.31 1.05 1.08 1.88
for-each (1e+2) 1000 10000 1020.69 1.02 1.02 0.95 1.11 1.02 1.03 1.41
Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+3) 1000 1000 649.24 0.65 0.65 0.64 0.67 0.65 0.65 0.70
for-rev (1e+3) 1000 1000 964.99 0.96 0.97 0.95 1.03 0.97 0.97 0.97
for-of (1e+3) 1000 1000 826.90 0.83 0.81 0.79 7.98 0.81 0.85 27.48
for-iter (1e+3) 1000 1000 965.97 0.97 0.97 0.95 1.00 0.97 0.97 0.66
for-each (1e+3) 1000 1000 1044.17 1.04 1.04 1.04 1.15 1.04 1.04 1.47
Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+4) 1000 100 637.23 0.64 0.64 0.63 0.66 0.64 0.64 0.54
for-rev (1e+4) 1000 100 955.42 0.96 0.96 0.94 1.01 0.96 0.96 1.17
for-of (1e+4) 1000 100 798.15 0.80 0.80 0.78 0.84 0.80 0.80 1.02
for-iter (1e+4) 1000 100 956.19 0.96 0.96 0.94 1.00 0.96 0.96 0.39
for-each (1e+4) 1000 100 1038.34 1.04 1.03 1.01 1.16 1.03 1.04 1.08
Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+5) 1000 10 959.51 0.96 0.96 0.94 1.19 0.95 0.96 1.75
for-rev (1e+5) 1000 10 961.61 0.96 0.96 0.94 1.14 0.95 0.96 1.49
for-of (1e+5) 1000 10 957.20 0.96 0.96 0.94 1.02 0.95 0.96 1.00
for-iter (1e+5) 1000 10 962.33 0.96 0.96 0.94 1.25 0.95 0.96 1.80
for-each (1e+5) 1000 10 5464.19 5.46 5.45 5.30 5.82 5.40 5.53 1.76
Title Iter Size Total Mean Median Min Max Q1 Q3 SD%
for-fwd (1e+6) 1000 10 9635.94 9.64 9.62 9.41 10.47 9.60 9.65 0.79
for-rev (1e+6) 1000 10 9676.22 9.68 9.61 9.54 10.84 9.59 9.73 1.42
for-of (1e+6) 1000 10 9657.31 9.66 9.62 9.58 12.48 9.59 9.67 1.59
for-iter (1e+6) 1000 10 9645.22 9.65 9.62 9.58 10.42 9.59 9.67 0.88
for-each (1e+6) 1000 10 45568.20 45.57 45.36 44.77 50.86 45.19 45.66 1.40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment