Skip to the code

Diff two builds' bundle sizes

Every pull request can change what the tea shop's visitors download, so CI posts a table of bundle sizes on each one (compressed-size-action does it on GitHub). Write bundleDiff(before, after). Each argument is one build's chunks, [{ name, bytes }], and names are unique within a build. A chunk in both builds has the same name in both.

Return { added, removed, changed, total }. added are the chunks only in after and removed the chunks only in before, each as { name, bytes }, largest first. changed are the chunks in both builds whose size changed, as { name, before, after, delta, percent }: delta is after - before in bytes, and percent is delta / before * 100 rounded to one decimal with Math.round(percent * 10) / 10. Sort changed by the size of the change, whichever way it went, largest first. A chunk whose size didn't change isn't listed. When two chunks tie in any list, order them by name with <.

total is { before, after, delta, percent } for the sum of each build's chunks, with delta and percent worked out the same way. A percent, in changed or in total, is null when the size before was 0. Leave the arrays you were given as they were.

bundleDiff(
  [{ name: "main.js", bytes: 120000 }, { name: "menu.js", bytes: 40000 }, { name: "legacy-polyfills.js", bytes: 30000 }],
  [{ name: "main.js", bytes: 126000 }, { name: "menu.js", bytes: 32000 }, { name: "reviews.js", bytes: 18000 }],
);
// {
//   added: [{ name: "reviews.js", bytes: 18000 }],
//   removed: [{ name: "legacy-polyfills.js", bytes: 30000 }],
//   changed: [
//     { name: "menu.js", before: 40000, after: 32000, delta: -8000, percent: -20 },
//     { name: "main.js", before: 120000, after: 126000, delta: 6000, percent: 5 },
//   ],
//   total: { before: 190000, after: 176000, delta: -14000, percent: -7.4 },
// }

What the tests check

  1. a chunk only in the new build is added, and one only in the old build is removed
    const before = [{ name: "main.js", bytes: 120000 }, { name: "legacy-polyfills.js", bytes: 30000 }, { name: "carousel.js", bytes: 12000 }];
    const after = [{ name: "main.js", bytes: 120000 }, { name: "reviews.js", bytes: 18000 }, { name: "cart.js", bytes: 22000 }];
    const diff = bundleDiff(before, after);
    expect(diff.added).toEqual([{ name: "cart.js", bytes: 22000 }, { name: "reviews.js", bytes: 18000 }]);
    expect(diff.removed).toEqual([{ name: "legacy-polyfills.js", bytes: 30000 }, { name: "carousel.js", bytes: 12000 }]);
    expect(diff.changed).toEqual([]);
    
  2. a changed chunk has its delta, and its percent rounded to one decimal
    const diff = bundleDiff(
      [{ name: "menu.js", bytes: 30000 }, { name: "main.js", bytes: 140000 }],
      [{ name: "menu.js", bytes: 31000 }, { name: "main.js", bytes: 133000 }],
    );
    expect(diff.changed).toEqual([
      { name: "main.js", before: 140000, after: 133000, delta: -7000, percent: -5 },
      { name: "menu.js", before: 30000, after: 31000, delta: 1000, percent: 3.3 },
    ]);
    
  3. sorts changes by how big they are, whichever way they went
    const before = [{ name: "checkout.js", bytes: 40000 }, { name: "menu.js", bytes: 50000 }, { name: "reviews.js", bytes: 20000 }];
    const after = [{ name: "checkout.js", bytes: 44000 }, { name: "menu.js", bytes: 41000 }, { name: "reviews.js", bytes: 26000 }];
    expect(bundleDiff(before, after).changed.map((chunk) => [chunk.name, chunk.delta, chunk.percent])).toEqual([
      ["menu.js", -9000, -18],
      ["reviews.js", 6000, 30],
      ["checkout.js", 4000, 10],
    ]);
    
  4. chunks that didn't change aren't listed, and ties go by name
    const before = [{ name: "vendor.js", bytes: 200000 }, { name: "tea-list.js", bytes: 9000 }, { name: "tea-card.js", bytes: 8000 }];
    const after = [
      { name: "vendor.js", bytes: 200000 },
      { name: "tea-list.js", bytes: 10000 },
      { name: "tea-card.js", bytes: 7000 },
      { name: "gift-card.js", bytes: 5000 },
      { name: "coupon.js", bytes: 5000 },
    ];
    const diff = bundleDiff(before, after);
    expect(diff.changed).toEqual([
      { name: "tea-card.js", before: 8000, after: 7000, delta: -1000, percent: -12.5 },
      { name: "tea-list.js", before: 9000, after: 10000, delta: 1000, percent: 11.1 },
    ]);
    expect(diff.added).toEqual([{ name: "coupon.js", bytes: 5000 }, { name: "gift-card.js", bytes: 5000 }]);
    
  5. the total compares the sums of both builds, added and removed chunks included
    const before = [{ name: "main.js", bytes: 120000 }, { name: "menu.js", bytes: 40000 }, { name: "legacy-polyfills.js", bytes: 30000 }];
    const after = [{ name: "main.js", bytes: 126000 }, { name: "menu.js", bytes: 32000 }, { name: "reviews.js", bytes: 18000 }];
    expect(bundleDiff(before, after)).toEqual({
      added: [{ name: "reviews.js", bytes: 18000 }],
      removed: [{ name: "legacy-polyfills.js", bytes: 30000 }],
      changed: [
        { name: "menu.js", before: 40000, after: 32000, delta: -8000, percent: -20 },
        { name: "main.js", before: 120000, after: 126000, delta: 6000, percent: 5 },
      ],
      total: { before: 190000, after: 176000, delta: -14000, percent: -7.4 },
    });
    
  6. a chunk or a build that started at 0 bytes has no percent
    const diff = bundleDiff([{ name: "empty.js", bytes: 0 }], [{ name: "empty.js", bytes: 2000 }]);
    expect(diff.changed).toEqual([{ name: "empty.js", before: 0, after: 2000, delta: 2000, percent: null }]);
    expect(diff.total).toEqual({ before: 0, after: 2000, delta: 2000, percent: null });
    expect(bundleDiff([], [])).toEqual({ added: [], removed: [], changed: [], total: { before: 0, after: 0, delta: 0, percent: null } });
    
  7. leaves the arrays it was given as they were
    const before = [{ name: "menu.js", bytes: 1000 }, { name: "cart.js", bytes: 3000 }];
    const after = [{ name: "cart.js", bytes: 1000 }, { name: "reviews.js", bytes: 9000 }, { name: "about.js", bytes: 4000 }];
    bundleDiff(before, after);
    expect(before).toEqual([{ name: "menu.js", bytes: 1000 }, { name: "cart.js", bytes: 3000 }]);
    expect(after).toEqual([{ name: "cart.js", bytes: 1000 }, { name: "reviews.js", bytes: 9000 }, { name: "about.js", bytes: 4000 }]);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).