Conditional arrays

Benchmark created on


Description

Test of differrent methods for creating array with conditional elements

Setup

const kind = 'column'

Test runner

Ready to run.

Testing in
TestOps/sec
Filtering
[
  (kind === "crosshair" || kind === "column") && {
    range: "col-before",
  },
  (kind === "crosshair" || kind === "column") && {
    range: "col-after",
  },
  (kind === "crosshair" || kind === "row") && {
    range: "row-before",
  },
  (kind === "crosshair" || kind === "row") && {
    range: "row-after",
  },
].filter(Boolean);
ready
Spread
[
  ...(kind === "crosshair" || kind === "column"
    ? [
        {
          range: "col-before",
        },
      ]
    : []),
  ...(kind === "crosshair" || kind === "column"
    ? [
        {
          range: "col-after",
        },
      ]
    : []),
  ...(kind === "crosshair" || kind === "row"
    ? [
        {
          range: "row-before",
        },
      ]
    : []),
  ...(kind === "crosshair" || kind === "row"
    ? [
        {
          range: "row-after",
        },
      ]
    : []),
];

ready
Includes & Filtering
[
  ["crosshair", "column"].includes(kind) && {
    range: "col-before",
  },
  ["crosshair", "column"].includes(kind) && {
    range: "col-after",
  },
  ["crosshair", "row"].includes(kind) && {
    range: "row-before",
  },
  ["crosshair", "row"].includes(kind) && {
    range: "row-after",
  },
].filter(Boolean);
ready
Static if
const ca = { range: "col-after" };
const cb = { range: "col-before" };
const rb = { range: "row-before" };
const ra = { range: "row-after" };

if (kind === 'crosshair') {
	return [ca,cb,rb,ra];
} else if (kind === 'row') {
	return [ra,rb];
} else if (kind === 'column') {
	return [ca,cb];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.