Skip to the code

The cost of each third party

Third-party scripts (reviews widgets, chat, analytics, ads) cost bytes, and worse, main-thread time. Write thirdPartyCost(pageUrl, resources, tasks, entities). resources are the page's requests, { url, type, transferSize } with the size in bytes. tasks are its main-thread tasks, { duration, urls }: how long the task ran, in ms, and the URLs found in its stack. Return what each third party cost.

A task belongs to one URL, as in Lighthouse's bootup-time audit, whose numbers the third-party-web project aggregates: the first of its urls that the page loaded as a script (a resource with type: "script"), or failing that its first URL. A task with no URLs belongs to no one. A resource belongs to its own url.

entities maps origins to the company behind them, the way third-party-web assigns each origin to an entity: { "https://fonts.googleapis.com": "Google Fonts", "https://fonts.gstatic.com": "Google Fonts" }. An origin that isn't in the map is an entity of its own, named by the origin. The page's entity, the one for pageUrl's origin, is first party and isn't reported, so mapping https://cdn.leafandkettle.com to the same name as the shop makes its CDN first party too.

Return [{ entity, transferSize, mainThreadTime }], one for each third-party entity with at least one resource or task, adding up its resources' transferSize and its tasks' duration. Sort by mainThreadTime, largest first, then by transferSize, largest first, then by entity with <.

thirdPartyCost(
  "https://leafandkettle.com/menu",
  [
    { url: "https://leafandkettle.com/js/menu.js", type: "script", transferSize: 40000 },
    { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 52000 },
    { url: "https://fonts.googleapis.com/css2?family=Lora", type: "stylesheet", transferSize: 1200 },
    { url: "https://fonts.gstatic.com/s/lora.woff2", type: "font", transferSize: 24000 },
  ],
  [
    { duration: 180, urls: ["https://widgets.reviewly.example/embed.js"] },
    { duration: 95, urls: ["https://leafandkettle.com/js/menu.js"] },
  ],
  { "https://fonts.googleapis.com": "Google Fonts", "https://fonts.gstatic.com": "Google Fonts" },
);
// [
//   { entity: "https://widgets.reviewly.example", transferSize: 52000, mainThreadTime: 180 },
//   { entity: "Google Fonts", transferSize: 25200, mainThreadTime: 0 },
// ]

What the tests check

  1. adds up bytes and main-thread time per third-party origin, leaving out the page's own
    const resources = [
      { url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 },
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 40000 },
      { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 52000 },
      { url: "https://widgets.reviewly.example/styles.css", type: "stylesheet", transferSize: 8000 },
      { url: "https://chat.teahelp.example/widget.js", type: "script", transferSize: 30000 },
    ];
    const tasks = [
      { duration: 180, urls: ["https://widgets.reviewly.example/embed.js"] },
      { duration: 60, urls: ["https://chat.teahelp.example/widget.js"] },
      { duration: 95, urls: ["https://leafandkettle.com/js/app.js"] },
      { duration: 40, urls: ["https://widgets.reviewly.example/embed.js"] },
    ];
    expect(thirdPartyCost("https://leafandkettle.com/menu", resources, tasks, {})).toEqual([
      { entity: "https://widgets.reviewly.example", transferSize: 60000, mainThreadTime: 220 },
      { entity: "https://chat.teahelp.example", transferSize: 30000, mainThreadTime: 60 },
    ]);
    
  2. merges a company's origins into one entity
    const resources = [
      { url: "https://www.googletagmanager.com/gtag/js?id=G-TEA", type: "script", transferSize: 90000 },
      { url: "https://www.google-analytics.com/g/collect?v=2", type: "other", transferSize: 300 },
      { url: "https://fonts.googleapis.com/css2?family=Lora", type: "stylesheet", transferSize: 1200 },
      { url: "https://fonts.gstatic.com/s/lora.woff2", type: "font", transferSize: 24000 },
    ];
    const tasks = [{ duration: 210, urls: ["https://www.googletagmanager.com/gtag/js?id=G-TEA"] }];
    const entities = {
      "https://www.googletagmanager.com": "Google Tag Manager",
      "https://www.google-analytics.com": "Google Analytics",
      "https://fonts.googleapis.com": "Google Fonts",
      "https://fonts.gstatic.com": "Google Fonts",
    };
    expect(thirdPartyCost("https://leafandkettle.com/", resources, tasks, entities)).toEqual([
      { entity: "Google Tag Manager", transferSize: 90000, mainThreadTime: 210 },
      { entity: "Google Fonts", transferSize: 25200, mainThreadTime: 0 },
      { entity: "Google Analytics", transferSize: 300, mainThreadTime: 0 },
    ]);
    
  3. an origin mapped to the page's entity is first party
    const resources = [
      { url: "https://leafandkettle.com/", type: "document", transferSize: 18000 },
      { url: "https://cdn.leafandkettle.com/img/oolong.avif", type: "image", transferSize: 40000 },
      { url: "https://cdn.leafandkettle.com/js/vendor.js", type: "script", transferSize: 70000 },
      { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 52000 },
    ];
    const tasks = [
      { duration: 120, urls: ["https://cdn.leafandkettle.com/js/vendor.js"] },
      { duration: 80, urls: ["https://widgets.reviewly.example/embed.js"] },
    ];
    const entities = { "https://leafandkettle.com": "Leaf and Kettle", "https://cdn.leafandkettle.com": "Leaf and Kettle" };
    expect(thirdPartyCost("https://leafandkettle.com/", resources, tasks, entities)).toEqual([
      { entity: "https://widgets.reviewly.example", transferSize: 52000, mainThreadTime: 80 },
    ]);
    
  4. a task goes to the first of its URLs that's a script, or else to its first URL
    const resources = [
      { url: "https://leafandkettle.com/menu", type: "document", transferSize: 18000 },
      { url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 40000 },
      { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 52000 },
      { url: "https://ads.teapromo.example/frame.html", type: "document", transferSize: 15000 },
    ];
    const tasks = [
      { duration: 150, urls: ["https://leafandkettle.com/menu", "https://widgets.reviewly.example/embed.js"] },
      { duration: 70, urls: ["https://ads.teapromo.example/frame.html"] },
      { duration: 30, urls: [] },
      { duration: 90, urls: ["https://leafandkettle.com/js/app.js", "https://widgets.reviewly.example/embed.js"] },
    ];
    expect(thirdPartyCost("https://leafandkettle.com/menu", resources, tasks, {})).toEqual([
      { entity: "https://widgets.reviewly.example", transferSize: 52000, mainThreadTime: 150 },
      { entity: "https://ads.teapromo.example", transferSize: 15000, mainThreadTime: 70 },
    ]);
    
  5. a third party with only main-thread time, or only bytes, is listed
    const resources = [{ url: "https://pixel.tracker.example/p.gif", type: "image", transferSize: 43 }];
    const tasks = [{ duration: 45, urls: ["https://cdn.chatnow.example/boot.js"] }];
    expect(thirdPartyCost("https://leafandkettle.com/", resources, tasks, {})).toEqual([
      { entity: "https://cdn.chatnow.example", transferSize: 0, mainThreadTime: 45 },
      { entity: "https://pixel.tracker.example", transferSize: 43, mainThreadTime: 0 },
    ]);
    
  6. sorts by main-thread time, then bytes, then name
    const resources = [
      { url: "https://widgets.reviewly.example/embed.js", type: "script", transferSize: 5000 },
      { url: "https://cdn.chatnow.example/boot.js", type: "script", transferSize: 9000 },
      { url: "https://ads.teapromo.example/ad.js", type: "script", transferSize: 9000 },
      { url: "https://video.steepcast.example/intro.mp4", type: "media", transferSize: 100000 },
    ];
    const tasks = [
      { duration: 100, urls: ["https://ads.teapromo.example/ad.js"] },
      { duration: 100, urls: ["https://widgets.reviewly.example/embed.js"] },
      { duration: 100, urls: ["https://cdn.chatnow.example/boot.js"] },
    ];
    const entities = {
      "https://widgets.reviewly.example": "Reviewly",
      "https://cdn.chatnow.example": "ChatNow",
      "https://ads.teapromo.example": "TeaPromo",
      "https://video.steepcast.example": "SteepCast",
    };
    expect(thirdPartyCost("https://leafandkettle.com/", resources, tasks, entities).map((item) => item.entity)).toEqual(["ChatNow", "TeaPromo", "Reviewly", "SteepCast"]);
    
  7. a page with no third parties is an empty array
    const resources = [{ url: "https://leafandkettle.com/js/app.js", type: "script", transferSize: 40000 }];
    const tasks = [{ duration: 95, urls: ["https://leafandkettle.com/js/app.js"] }];
    expect(thirdPartyCost("https://leafandkettle.com/", resources, tasks, {})).toEqual([]);
    expect(thirdPartyCost("https://leafandkettle.com/", [], [], {})).toEqual([]);
    

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).