Skip to content

Instantly share code, notes, and snippets.

@zachlankton
Created October 22, 2022 15:41
Show Gist options
  • Save zachlankton/14ce2d0e7b632d4341eb4f86ea2d126a to your computer and use it in GitHub Desktop.
Save zachlankton/14ce2d0e7b632d4341eb4f86ea2d126a to your computer and use it in GitHub Desktop.
twMerge Performance Test

The Test:

import { noop } from "lodash";
import { twMerge } from "tailwind-merge";

function testTWMerge() {
  const variantClasses =
    "inline-flex justify-center rounded-md border border-transparent bg-primary py-2 px-4 text-sm font-medium text-primaryText shadow-sm hover:ring-2 hover:ring-textShade-900 focus:outline-none focus:ring-2 focus:ring-secondary focus:ring-offset-2 dark:bg-dPrimary dark:text-dPrimaryText dark:hover:ring-dTextShade-900";

  const twmergeIterations = 1000000; //     1,000,000
  const stringConcatIters = 100000000; // 100,000,000
  const twmergeStart = performance.now();
  for (let i = 0; i < twmergeIterations; i++) {
    const x = twMerge(variantClasses, "mx-4");
    noop(x);
  }
  const twmergeEnd = performance.now();

  const twmergeStart2 = performance.now();
  for (let i = 0; i < twmergeIterations; i++) {
    const x = twMerge(variantClasses, variantClasses);
    noop(x);
  }
  const twmergeEnd2 = performance.now();

  const stringConcatStart = performance.now();
  for (let i = 0; i < stringConcatIters; i++) {
    const y = `${variantClasses} mx-4`;
    noop(y);
  }
  const stringConcatEnd = performance.now();

  console.log(
    "twMerge:",
    ((twmergeEnd - twmergeStart) / twmergeIterations) * 1000000,
    "nanoseconds"
  );
  console.log(
    "twMerge2:",
    ((twmergeEnd2 - twmergeStart2) / twmergeIterations) * 1000000,
    "nanoseconds"
  );
  console.log(
    "stringConcat:",
    ((stringConcatEnd - stringConcatStart) / stringConcatIters) * 1000000,
    "nanoseconds"
  );
}
export { testTWMerge }

The Results:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment