benchmarks.js

JavaScript source for Some JavaScript performance comparisons, 678 lines. Raw file

// Timing tests for "Some JavaScript performance comparisons".
//
// Each table in the article has a data-bench attribute naming one of the test
// lists below. Every test has a setup, which runs before the timing starts,
// and a run, which is the part that's timed. A test runs five times, with a
// fresh setup each time, and the result is the average of the five.
//
// The typed array tests are made from source text with new Function, one
// function per type, so each one stays specialised to its own type instead of
// sharing code (and the engine's type feedback) with every other type.
'use strict';

(function () {
  const N = 1000000;
  const RUNS = 5;

  // Every run's result goes here, so the engine can't throw the work away
  let sink;

  // ---------------------------------------------------------------------------
  // Data made in the setups

  const arrayOfNumbers = () => {
    const a = [];
    for (let i = 0; i < N; i++) a.push(i);
    return a;
  };

  const arrayOfStrings = (from) => {
    const a = [];
    for (let i = 0; i < N; i++) a.push('key' + (from + i));
    return a;
  };

  const arrayOfObjects = () => {
    const a = [];
    for (let i = 0; i < N; i++) a.push({ i: i });
    return a;
  };

  const mapOf = (keys) => {
    const m = new Map();
    for (let i = 0; i < N; i++) m.set(keys[i], i);
    return m;
  };

  const setOf = (keys) => {
    const s = new Set();
    for (let i = 0; i < N; i++) s.add(keys[i]);
    return s;
  };

  const objectOf = (keys) => {
    const o = {};
    for (let i = 0; i < N; i++) o[keys[i]] = i;
    return o;
  };

  // ---------------------------------------------------------------------------
  // Typed arrays

  const TYPED = [
    'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array',
    'Float16Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array',
  ];

  const isBig = (type) => type.startsWith('Big');
  const supported = (type) => () => typeof globalThis[type] === 'function';

  // One function per type, from source text with $T for the type, $V for the
  // value to store at index i and $Z for zero
  const typedFunction = (type, params, body) => {
    const source = body
      .replace(/\$T/g, type)
      .replace(/\$V/g, isBig(type) ? 'BigInt(i)' : 'i')
      .replace(/\$Z/g, isBig(type) ? '0n' : '0');
    return new Function(...params, source);
  };

  const filledTyped = (type) => {
    const fill = typedFunction(type, ['N'], 'const a = new $T(N); for (let i = 0; i < N; i++) a[i] = $V; return a;');
    return fill(N);
  };

  // ---------------------------------------------------------------------------
  // The tests, one list per table

  const TESTS = {};

  TESTS.controls = [
    {
      name: '<code>for</code> loop',
      run: () => { for (let i = 0; i < N; i++) {} },
    },
    {
      name: '<code>while</code> loop incrementing a counter',
      run: () => { let i = 0; while (i < N) { i++; } return i; },
    },
    {
      name: '<code>do</code> … <code>while</code> loop incrementing a counter',
      run: () => { let i = 0; do { i++; } while (i < N); return i; },
    },
    {
      name: '<code>for</code> … <code>of</code> over an array',
      setup: arrayOfNumbers,
      run: (a) => { for (const x of a) {} },
    },
    {
      name: '<code>.forEach</code> on an array',
      setup: arrayOfNumbers,
      run: (a) => { a.forEach(() => {}); },
    },
    {
      name: '<code>.map</code> on an array',
      setup: arrayOfNumbers,
      run: (a) => a.map(() => {}),
    },
  ];

  TESTS.filling = [
    {
      name: '<code>push</code> onto an empty array',
      run: () => { const a = []; for (let i = 0; i < N; i++) a.push(i); return a; },
    },
    {
      name: '<code>push</code> of <code>Math.random()</code> onto an empty array',
      run: () => { const a = []; for (let i = 0; i < N; i++) a.push(Math.random()); return a; },
    },
    {
      name: 'Setting by index on an empty array',
      run: () => { const a = []; for (let i = 0; i < N; i++) a[i] = i; return a; },
    },
    {
      name: 'Setting by index on <code>new Array(N)</code>',
      run: () => { const a = new Array(N); for (let i = 0; i < N; i++) a[i] = i; return a; },
    },
    {
      name: '<code>Array.from({ length: N }, (_, i) =&gt; i)</code>',
      run: () => Array.from({ length: N }, (_, i) => i),
    },
    {
      name: '<code>new Array(N).fill(0)</code>',
      run: () => new Array(N).fill(0),
    },
    {
      name: '<code>Map</code> with integer keys, <code>m.set(i, i)</code>',
      run: () => { const m = new Map(); for (let i = 0; i < N; i++) m.set(i, i); return m; },
    },
    ...TYPED.map((type) => ({
      name: `Setting by index on <code>new ${type}(N)</code>`,
      supported: supported(type),
      make: () => typedFunction(type, ['N'], 'const a = new $T(N); for (let i = 0; i < N; i++) a[i] = $V; return a;'),
      run: (s, f) => f(N),
    })),
  ];

  TESTS.summing = [
    {
      name: '<code>for</code> loop',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; for (let i = 0; i < a.length; i++) sum += a[i]; return sum; },
    },
    {
      name: '<code>for</code> loop with the length read once',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; for (let i = 0, n = a.length; i < n; i++) sum += a[i]; return sum; },
    },
    {
      name: '<code>for</code> loop counting down',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; for (let i = a.length - 1; i >= 0; i--) sum += a[i]; return sum; },
    },
    {
      name: '<code>while</code> loop',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; let i = 0; while (i < a.length) sum += a[i++]; return sum; },
    },
    {
      name: '<code>for</code> … <code>of</code>',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; for (const x of a) sum += x; return sum; },
    },
    {
      name: '<code>.forEach</code>',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; a.forEach((x) => { sum += x; }); return sum; },
    },
    {
      name: '<code>.reduce</code>',
      setup: arrayOfNumbers,
      run: (a) => a.reduce((sum, x) => sum + x, 0),
    },
    {
      name: '<code>for</code> … <code>in</code>',
      setup: arrayOfNumbers,
      run: (a) => { let sum = 0; for (const i in a) sum += a[i]; return sum; },
    },
    ...TYPED.map((type) => ({
      name: `<code>for</code> loop over ${type.startsWith('Int') ? 'an' : 'a'} <code>${type}</code>`,
      supported: supported(type),
      setup: () => filledTyped(type),
      make: () => typedFunction(type, ['a'], 'let sum = $Z; for (let i = 0; i < a.length; i++) sum += a[i]; return sum;'),
      run: (a, f) => f(a),
    })),
  ];

  TESTS['copying-arrays'] = [
    {
      name: '<code>push</code> in a <code>for</code> loop',
      setup: arrayOfNumbers,
      run: (a) => { const b = []; for (let i = 0; i < a.length; i++) b.push(a[i]); return b; },
    },
    {
      name: 'Setting by index on <code>new Array(N)</code>',
      setup: arrayOfNumbers,
      run: (a) => { const b = new Array(a.length); for (let i = 0; i < a.length; i++) b[i] = a[i]; return b; },
    },
    {
      name: '<code>.slice()</code>',
      setup: arrayOfNumbers,
      run: (a) => a.slice(),
    },
    {
      name: 'Spread, <code>[...a]</code>',
      setup: arrayOfNumbers,
      run: (a) => [...a],
    },
    {
      name: '<code>Array.from(a)</code>',
      setup: arrayOfNumbers,
      run: (a) => Array.from(a),
    },
    {
      name: '<code>.concat()</code>',
      setup: arrayOfNumbers,
      run: (a) => a.concat(),
    },
    {
      name: '<code>.map(x =&gt; x)</code>',
      setup: arrayOfNumbers,
      run: (a) => a.map((x) => x),
    },
    {
      name: '<code>structuredClone(a)</code>',
      setup: arrayOfNumbers,
      run: (a) => structuredClone(a),
    },
  ];

  TESTS['copying-typed-arrays'] = [];
  for (const type of TYPED) {
    TESTS['copying-typed-arrays'].push(
      {
        name: `<code>${type}</code>, <code>for</code> loop into <code>new ${type}(N)</code>`,
        supported: supported(type),
        setup: () => filledTyped(type),
        make: () => typedFunction(type, ['a'], 'const b = new $T(a.length); for (let i = 0; i < a.length; i++) b[i] = a[i]; return b;'),
        run: (a, f) => f(a),
      },
      {
        name: `<code>${type}</code>, <code>new ${type}(a)</code>`,
        supported: supported(type),
        setup: () => filledTyped(type),
        make: () => typedFunction(type, ['a'], 'return new $T(a);'),
        run: (a, f) => f(a),
      },
      {
        name: `<code>${type}</code>, <code>.set(a)</code> on <code>new ${type}(N)</code>`,
        supported: supported(type),
        setup: () => filledTyped(type),
        make: () => typedFunction(type, ['a'], 'const b = new $T(a.length); b.set(a); return b;'),
        run: (a, f) => f(a),
      },
    );
  }

  TESTS.maps = [
    {
      name: '<code>set</code> with integer keys',
      setup: () => new Map(),
      run: (m) => { for (let i = 0; i < N; i++) m.set(i, i); return m; },
    },
    {
      name: '<code>set</code> with string keys',
      setup: () => ({ m: new Map(), keys: arrayOfStrings(0) }),
      run: ({ m, keys }) => { for (let i = 0; i < N; i++) m.set(keys[i], i); return m; },
    },
    {
      name: '<code>has</code> with integer keys',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { let found = 0; for (let i = 0; i < N; i++) if (m.has(i)) found++; return found; },
    },
    {
      name: '<code>has</code> with string keys',
      setup: () => { const keys = arrayOfStrings(0); return { m: mapOf(keys), keys }; },
      run: ({ m, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (m.has(keys[i])) found++; return found; },
    },
    {
      name: '<code>has</code> with integer keys that aren\'t there',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { let found = 0; for (let i = N; i < 2 * N; i++) if (m.has(i)) found++; return found; },
    },
    {
      name: '<code>get</code> with integer keys',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { let sum = 0; for (let i = 0; i < N; i++) sum += m.get(i); return sum; },
    },
    {
      name: '<code>get</code> with string keys',
      setup: () => { const keys = arrayOfStrings(0); return { m: mapOf(keys), keys }; },
      run: ({ m, keys }) => { let sum = 0; for (let i = 0; i < N; i++) sum += m.get(keys[i]); return sum; },
    },
    {
      name: '<code>for</code> … <code>of</code> over the values',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { let sum = 0; for (const v of m.values()) sum += v; return sum; },
    },
    {
      name: '<code>delete</code> with integer keys',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { for (let i = 0; i < N; i++) m.delete(i); return m; },
    },
    {
      name: '<code>delete</code> with string keys',
      setup: () => { const keys = arrayOfStrings(0); return { m: mapOf(keys), keys }; },
      run: ({ m, keys }) => { for (let i = 0; i < N; i++) m.delete(keys[i]); return m; },
    },
    {
      name: '<code>clear()</code>',
      setup: () => mapOf(arrayOfNumbers()),
      run: (m) => { m.clear(); return m; },
    },
  ];

  TESTS.sets = [
    {
      name: '<code>add</code> with integers',
      setup: () => new Set(),
      run: (s) => { for (let i = 0; i < N; i++) s.add(i); return s; },
    },
    {
      name: '<code>add</code> with strings',
      setup: () => ({ s: new Set(), keys: arrayOfStrings(0) }),
      run: ({ s, keys }) => { for (let i = 0; i < N; i++) s.add(keys[i]); return s; },
    },
    {
      name: '<code>has</code> with integers',
      setup: () => setOf(arrayOfNumbers()),
      run: (s) => { let found = 0; for (let i = 0; i < N; i++) if (s.has(i)) found++; return found; },
    },
    {
      name: '<code>has</code> with strings',
      setup: () => { const keys = arrayOfStrings(0); return { s: setOf(keys), keys }; },
      run: ({ s, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (s.has(keys[i])) found++; return found; },
    },
    {
      name: '<code>has</code> with integers that aren\'t there',
      setup: () => setOf(arrayOfNumbers()),
      run: (s) => { let found = 0; for (let i = N; i < 2 * N; i++) if (s.has(i)) found++; return found; },
    },
    {
      name: '<code>for</code> … <code>of</code> over the values',
      setup: () => setOf(arrayOfNumbers()),
      run: (s) => { let sum = 0; for (const v of s) sum += v; return sum; },
    },
    {
      name: '<code>delete</code> with integers',
      setup: () => setOf(arrayOfNumbers()),
      run: (s) => { for (let i = 0; i < N; i++) s.delete(i); return s; },
    },
    {
      name: '<code>delete</code> with strings',
      setup: () => { const keys = arrayOfStrings(0); return { s: setOf(keys), keys }; },
      run: ({ s, keys }) => { for (let i = 0; i < N; i++) s.delete(keys[i]); return s; },
    },
  ];

  TESTS.objects = [
    {
      name: 'Setting integer keys, <code>o[i] = i</code>',
      setup: () => ({}),
      run: (o) => { for (let i = 0; i < N; i++) o[i] = i; return o; },
    },
    {
      name: 'Setting string keys, <code>o[key] = i</code>',
      setup: () => ({ o: {}, keys: arrayOfStrings(0) }),
      run: ({ o, keys }) => { for (let i = 0; i < N; i++) o[keys[i]] = i; return o; },
    },
    {
      name: '<code>in</code> with integer keys',
      setup: () => objectOf(arrayOfNumbers()),
      run: (o) => { let found = 0; for (let i = 0; i < N; i++) if (i in o) found++; return found; },
    },
    {
      name: '<code>in</code> with string keys',
      setup: () => { const keys = arrayOfStrings(0); return { o: objectOf(keys), keys }; },
      run: ({ o, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (keys[i] in o) found++; return found; },
    },
    {
      name: '<code>in</code> with string keys that aren\'t there',
      setup: () => ({ o: objectOf(arrayOfStrings(0)), keys: arrayOfStrings(N) }),
      run: ({ o, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (keys[i] in o) found++; return found; },
    },
    {
      name: 'Reading integer keys, <code>o[i]</code>',
      setup: () => objectOf(arrayOfNumbers()),
      run: (o) => { let sum = 0; for (let i = 0; i < N; i++) sum += o[i]; return sum; },
    },
    {
      name: 'Reading string keys, <code>o[key]</code>',
      setup: () => { const keys = arrayOfStrings(0); return { o: objectOf(keys), keys }; },
      run: ({ o, keys }) => { let sum = 0; for (let i = 0; i < N; i++) sum += o[keys[i]]; return sum; },
    },
    {
      name: '<code>for</code> … <code>in</code> over string keys',
      setup: () => objectOf(arrayOfStrings(0)),
      run: (o) => { let sum = 0; for (const k in o) sum += o[k]; return sum; },
    },
    {
      name: '<code>delete</code> with integer keys',
      setup: () => objectOf(arrayOfNumbers()),
      run: (o) => { for (let i = 0; i < N; i++) delete o[i]; return o; },
    },
    {
      name: '<code>delete</code> with string keys',
      setup: () => { const keys = arrayOfStrings(0); return { o: objectOf(keys), keys }; },
      run: ({ o, keys }) => { for (let i = 0; i < N; i++) delete o[keys[i]]; return o; },
    },
  ];

  TESTS.weak = [
    {
      name: '<code>WeakMap</code>, <code>set</code>',
      setup: () => ({ m: new WeakMap(), keys: arrayOfObjects() }),
      run: ({ m, keys }) => { for (let i = 0; i < N; i++) m.set(keys[i], i); return m; },
    },
    {
      name: '<code>WeakMap</code>, <code>has</code>',
      setup: () => { const keys = arrayOfObjects(); const m = new WeakMap(); for (let i = 0; i < N; i++) m.set(keys[i], i); return { m, keys }; },
      run: ({ m, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (m.has(keys[i])) found++; return found; },
    },
    {
      name: '<code>WeakMap</code>, <code>get</code>',
      setup: () => { const keys = arrayOfObjects(); const m = new WeakMap(); for (let i = 0; i < N; i++) m.set(keys[i], i); return { m, keys }; },
      run: ({ m, keys }) => { let sum = 0; for (let i = 0; i < N; i++) sum += m.get(keys[i]); return sum; },
    },
    {
      name: '<code>WeakMap</code>, <code>delete</code>',
      setup: () => { const keys = arrayOfObjects(); const m = new WeakMap(); for (let i = 0; i < N; i++) m.set(keys[i], i); return { m, keys }; },
      run: ({ m, keys }) => { for (let i = 0; i < N; i++) m.delete(keys[i]); return m; },
    },
    {
      name: '<code>WeakSet</code>, <code>add</code>',
      setup: () => ({ s: new WeakSet(), keys: arrayOfObjects() }),
      run: ({ s, keys }) => { for (let i = 0; i < N; i++) s.add(keys[i]); return s; },
    },
    {
      name: '<code>WeakSet</code>, <code>has</code>',
      setup: () => { const keys = arrayOfObjects(); const s = new WeakSet(); for (let i = 0; i < N; i++) s.add(keys[i]); return { s, keys }; },
      run: ({ s, keys }) => { let found = 0; for (let i = 0; i < N; i++) if (s.has(keys[i])) found++; return found; },
    },
    {
      name: '<code>WeakSet</code>, <code>delete</code>',
      setup: () => { const keys = arrayOfObjects(); const s = new WeakSet(); for (let i = 0; i < N; i++) s.add(keys[i]); return { s, keys }; },
      run: ({ s, keys }) => { for (let i = 0; i < N; i++) s.delete(keys[i]); return s; },
    },
    {
      name: 'For comparison, a <code>Map</code> with the same object keys, <code>set</code>',
      setup: () => ({ m: new Map(), keys: arrayOfObjects() }),
      run: ({ m, keys }) => { for (let i = 0; i < N; i++) m.set(keys[i], i); return m; },
    },
  ];

  // ---------------------------------------------------------------------------
  // My results, in milliseconds, in the same order as the tests above: a
  // MacBook Pro with an Apple M2 Max, Chrome 156, every table with Run all

  const MINE = {
    controls: [0.88, 0.78, 0.86, 12.0, 6.38, 11.1],
    filling: [
      2.98, 8.60, 3.08, 1.54, 30.8, 0.50, 52.5,
      1.14, 1.16, 1.80, 1.20, 1.16, 1.32, 3.18, 1.24, 1.26, 1.68, 16.0, 15.5,
    ],
    summing: [
      1.58, 1.54, 1.38, 1.52, 9.92, 8.62, 8.28, 33.3,
      1.34, 1.04, 2.52, 1.12, 2.60, 3.08, 1.32, 2.66, 1.36, 2.34, 3.04, 10.1,
    ],
    'copying-arrays': [2.76, 1.60, 0.50, 0.44, 0.52, 0.46, 8.40, 20.0],
    'copying-typed-arrays': [
      1.22, 0.18, 0.18, // Int8Array
      1.28, 0.14, 0.14, // Uint8Array
      1.20, 0.14, 0.18, // Uint8ClampedArray
      1.26, 0.30, 0.24, // Int16Array
      1.30, 0.30, 0.28, // Uint16Array
      1.52, 0.50, 0.52, // Int32Array
      1.36, 0.54, 0.42, // Uint32Array
      1.36, 0.24, 0.28, // Float16Array
      1.60, 0.52, 0.48, // Float32Array
      1.80, 0.86, 1.04, // Float64Array
      1.78, 0.78, 0.78, // BigInt64Array
      1.80, 0.76, 0.70, // BigUint64Array
    ],
    maps: [47.6, 167, 22.5, 26.4, 30.0, 23.8, 27.3, 7.20, 42.4, 46.8, 0.02],
    sets: [45.5, 146, 22.4, 32.4, 28.0, 10.7, 37.6, 41.8],
    objects: [8.86, 181, 1.86, 36.2, 344, 1.60, 32.0, 214, 30.2, 68.4],
    weak: [97.7, 23.0, 17.3, 56.3, 84.0, 25.1, 58.5, 27.8],
  };

  // Which tests in a table do the same job, in the same order as the tests.
  // The fastest in each group is shown in green and the slowest in red, in my
  // column and in the reader's; a table that isn't listed here is all one group.
  const repeat = (group, count) => new Array(count).fill(group);
  const GROUPS = {
    filling: [...repeat('array or Map', 7), ...repeat('typed', TYPED.length)],
    summing: [...repeat('array', 8), ...repeat('typed', TYPED.length)],
    'copying-typed-arrays': TYPED.flatMap((type) => repeat(type, 3)),
    maps: ['set', 'set', 'has', 'has', 'has', 'get', 'get', 'iterate', 'delete', 'delete', 'clear'],
    sets: ['add', 'add', 'has', 'has', 'has', 'iterate', 'delete', 'delete'],
    objects: ['set', 'set', 'in', 'in', 'in', 'read', 'read', 'iterate', 'delete', 'delete'],
    weak: ['set', 'has', 'get', 'delete', 'set', 'has', 'delete', 'set'],
  };

  // Gives each cell the background for the fastest or slowest in its group,
  // out of the times there are so far (a test that hasn't run has no time)
  const highlight = (bench, times, cells) => {
    const groups = GROUPS[bench] || repeat('all', cells.length);
    for (const group of new Set(groups)) {
      const members = groups.map((g, i) => (g === group ? i : -1)).filter((i) => i >= 0);
      const timed = members.filter((i) => times[i] !== undefined);
      for (const i of members) cells[i].style.background = '';
      if (timed.length < 2) continue;
      const timedTimes = timed.map((i) => times[i]);
      const fastest = Math.min(...timedTimes);
      const slowest = Math.max(...timedTimes);
      for (const i of timed) {
        if (times[i] === fastest) cells[i].style.background = 'var(--ok-bg)';
        else if (times[i] === slowest) cells[i].style.background = 'var(--err-bg)';
      }
    }
  };

  // ---------------------------------------------------------------------------
  // Running

  // Lets the page draw the latest text before the next timed run blocks it
  const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0)));

  const format = (ms) => (ms < 10 ? ms.toFixed(2) : ms < 100 ? ms.toFixed(1) : ms.toFixed(0));

  const showResult = (cell, ms) => {
    const perEntry = (ms * 1e6) / N;
    cell.innerHTML = `<span class="nowrap">${format(ms)} ms total</span><br><small class="nowrap">${format(perEntry)} ns per entry</small>`;
  };

  // Times one test: a fresh setup for each run, then the average of the runs
  const runTest = async (test, cell) => {
    const f = test.make ? test.make() : undefined;
    let total = 0;
    for (let r = 1; r <= RUNS; r++) {
      cell.textContent = `Running ${r}/${RUNS}`;
      await nextFrame();
      const state = test.setup ? test.setup() : undefined;
      const start = performance.now();
      sink = test.run(state, f);
      total += performance.now() - start;
    }
    showResult(cell, total / RUNS);
    return total / RUNS;
  };

  let busy = false;
  let stopRequested = false;
  const allButtons = [];

  const setBusy = (value, except) => {
    busy = value;
    for (const button of allButtons) {
      if (button !== except && !button.dataset.unsupported) button.disabled = value;
    }
  };

  // Returns the average time, or undefined if the test failed
  const runSafely = async (test, cell) => {
    try {
      return await runTest(test, cell);
    } catch (error) {
      cell.textContent = `Failed: ${error.message}`;
      return undefined;
    }
  };

  const setUp = (figure) => {
    const bench = figure.dataset.bench;
    const tests = TESTS[bench];
    const tbody = figure.querySelector('tbody');
    const rows = [];
    const myCells = [];
    const resultCells = [];
    const readerTimes = [];

    // Runs one row, and colours the reader's fastest and slowest so far
    const runRow = async (index) => {
      readerTimes[index] = undefined;
      highlight(bench, readerTimes, resultCells);
      readerTimes[index] = await runSafely(rows[index].test, rows[index].result);
      highlight(bench, readerTimes, resultCells);
    };

    for (const test of tests) {
      const ok = !test.supported || test.supported();
      const tr = document.createElement('tr');
      const name = document.createElement('td');
      const mine = document.createElement('td');
      const result = document.createElement('td');
      const action = document.createElement('td');
      const button = document.createElement('button');
      name.innerHTML = test.name;
      const index = rows.length;
      const myMs = MINE[bench][index];
      if (myMs === undefined) mine.textContent = '-';
      else showResult(mine, myMs);
      result.textContent = ok ? 'Not run' : 'Not supported in this browser';
      button.className = 'small secondary';
      button.type = 'button';
      button.textContent = 'Run';
      if (!ok) {
        button.disabled = true;
        button.dataset.unsupported = 'true';
      }
      button.addEventListener('click', async () => {
        if (busy) return;
        setBusy(true);
        await runRow(index);
        setBusy(false);
      });
      action.appendChild(button);
      tr.append(name, mine, result, action);
      tbody.appendChild(tr);
      allButtons.push(button);
      rows.push({ test, ok, result });
      myCells.push(mine);
      resultCells.push(result);
    }
    highlight(bench, MINE[bench], myCells);

    // The Run all button under the table runs the tests one after another, and
    // turns into a Stop button, which stops after the test that's running
    const runAll = document.querySelector(`button[data-bench-run-all="${bench}"]`);
    allButtons.push(runAll);
    runAll.addEventListener('click', async () => {
      if (busy) {
        stopRequested = true;
        runAll.textContent = 'Stopping';
        runAll.disabled = true;
        return;
      }
      stopRequested = false;
      setBusy(true, runAll);
      runAll.textContent = 'Stop';
      for (let index = 0; index < rows.length; index++) {
        if (stopRequested) break;
        if (rows[index].ok) await runRow(index);
      }
      runAll.textContent = 'Run all';
      setBusy(false);
    });
  };

  const start = () => {
    for (const figure of document.querySelectorAll('figure[data-bench]')) setUp(figure);
  };

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start);
  else start();

  // Keeps the sink reachable, so nothing written to it can be optimised away
  window.benchmarkSink = () => sink;
})();