Repository: prettier/prettier
File: tests/unit/doc-printer.js (JavaScript)

Code before:
  expect(doc.parts.length).toBe(255 + 254);

  const { formatted: secondPrint } = printDocToString(doc, printOptions);

  expect(firstPrint).toBe(secondPrint);

  {
    // About 1000 lines, #3263
    const WORD = "word";
    const hugeParts = join(
      line,
      Array.from(
        { length: 1000 * Math.ceil(printOptions.printWidth / WORD.length) },
        () => WORD,
      ),
    );
    const orignalLength = hugeParts.length;

    const startTime = performance.now();
    const { formatted } = printDocToString(fill(hugeParts), printOptions);
    const endTime = performance.now();
    expect(hugeParts.length).toBe(orignalLength);

    const lines = formatted.split("\n");
    expect(lines.length).toBeGreaterThan(1000);
    expect(endTime - startTime).toBeLessThan(100);
  }
});

describe("time complexity to print fill()", () => {
  const baseSize = 3_000;
  const relativeMargin = 0.3;
  const baseTime = time(makeFill(baseSize));
  test.each([10_000, 20_000, 40_000])("numWords=%d", (numWords) => {
    const doc = makeFill(numWords);
    const ellapsed = time(doc);
    const ratio = numWords / baseSize;
    expect(ellapsed).toBeLessThan(baseTime * ratio * (1 + relativeMargin));
  });

  function makeFill(numWords) {
    const WORD = "word";
    const parts = Array.from({ length: numWords }, () => WORD);
    return fill(join(line, parts));
  }

  function time(doc) {
    const printOptions = { printWidth: 40, tabWidth: 2 };
    const start = performance.now();
    printDocToString(doc, printOptions);
    return performance.now() - start;

Review comment (suggestion):
On my machine, it was:
- 181.9 on origin/main
- 13.3 on this branch

It get x10~ faster so dividing by 10 should be safe.

Code after:
  expect(doc.parts.length).toBe(255 + 254);

  const { formatted: secondPrint } = printDocToString(doc, printOptions);

  expect(firstPrint).toBe(secondPrint);

  {
    // About 1000 lines, #3263
    const WORD = "word";
    const hugeParts = join(
      line,
      Array.from(
        { length: 1000 * Math.ceil(printOptions.printWidth / WORD.length) },
        () => WORD,
      ),
    );
    const orignalLength = hugeParts.length;

    const startTime = performance.now();
    const { formatted } = printDocToString(fill(hugeParts), printOptions);
    const endTime = performance.now();
    expect(hugeParts.length).toBe(orignalLength);

    const lines = formatted.split("\n");
    expect(lines.length).toBeGreaterThan(1000);
    expect(endTime - startTime).toBeLessThan(100);
  }
});

describe("`printDocToString` has linear time complexity at most to print fill()", () => {
  const baseSize = 3_000;
  const relativeMargin = 0.3;
  const baseTime = time(makeFill(baseSize));
  test.each([10_000, 20_000, 40_000])("numWords=%d", (numWords) => {
    const doc = makeFill(numWords);
    const ellapsed = time(doc);
    const ratio = numWords / baseSize;
    expect(ellapsed).toBeLessThan(baseTime * ratio * (1 + relativeMargin));
  });

  function makeFill(numWords) {
    const WORD = "word";
    const parts = Array.from({ length: numWords }, () => WORD);
    return fill(join(line, parts));
  }

  function time(doc) {
    const printOptions = { printWidth: 40, tabWidth: 2 };
    const start = performance.now();
    printDocToString(doc, printOptions);
    return performance.now() - start;