completefrontend

Write the codeMedium

Write a function that finds which animations aren't compositor-friendly.

The tea shop's style sheets have dozens of transitions and animations, and some of them stutter on cheap phones. Write findCostlyAnimations(animations), which sorts them by the most expensive rendering step they make the browser run in every frame. Each animation is { selector, properties }, where properties lists the CSS properties it animates. Return { layout, paint, composite }, three arrays of { selector, properties }.

Classify each property with these lists, which follow the common classification in web.dev's rendering guides and CSS Triggers-style tables. Browsers differ in the details (Chrome's compositor can also animate filter, background-color, and clip-path), so treat them as a rule of thumb that holds everywhere. Composite only: transform, translate, rotate, scale, opacity. Paint: color, background, background-color, background-image, background-position, background-size, border-color, border-radius, box-shadow, outline, outline-color, text-shadow, text-decoration-color. Layout: width, height, min-width, min-height, max-width, max-height, top, right, bottom, left, inset, margin, padding, border, border-width, font-size, font-weight, line-height, letter-spacing, and all, since a transition of all animates whatever changes.

A longhand costs what its shorthand costs: a name that starts with margin-, padding-, or inset- is layout, and a name that starts with border- is layout when it ends in -width and paint when it ends in -color or -radius. Compare names after trimming spaces and lowering their case. Every name you're given is on a list or is one of these longhands.

An animation goes into the group of its most expensive property, layout before paint before composite, and appears once. Its properties in the result are the ones that cost that step, trimmed and in lower case, in the order given, each once; for composite, that's all of them. Leave out an animation with no properties, and keep the input's order within each group.

findCostlyAnimations([
  { selector: ".basket-badge", properties: ["transform", "opacity"] },
  { selector: ".tea-card:hover", properties: ["box-shadow", "transform"] },
  { selector: ".reviews", properties: ["height", "background-color"] },
]);
// {
//   layout: [{ selector: ".reviews", properties: ["height"] }],
//   paint: [{ selector: ".tea-card:hover", properties: ["box-shadow"] }],
//   composite: [{ selector: ".basket-badge", properties: ["transform", "opacity"] }],
// }
Write this: solution.js
function findCostlyAnimations(animations) {
  // Today only the first property counts, and only a few exact names are known.
  const layout = [];
  const paint = [];
  const composite = [];
  for (const { selector, properties } of animations) {
    const [first] = properties;
    if (["width", "height", "top", "left"].includes(first)) layout.push({ selector, properties: [first] });
    else if (["color", "background-color", "box-shadow"].includes(first)) paint.push({ selector, properties: [first] });
    else composite.push({ selector, properties });
  }
  return { layout, paint, composite };
}
The tests your code must pass
  • sorts the menu's animations by the step they cost
  • an animation goes once into the group of its costliest property, with only the properties that cost that step
  • a longhand costs what its shorthand costs
  • a transition of all counts as layout
  • matches names whatever their case and spacing, and lists each once
  • no animations, or one with no properties, gives empty groups

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…