Measure at the Output, Not the Schematic

Measure at the output, not the schematic: Clarity's agent cut 1,257 gzip bytes, then watched a helper that saved 264 raw bytes cost 23 gzip.

Measure at the output, not the schematic. Microsoft Clarity’s agent cut 1,257 gzip bytes off clarity.js, then watched a helper that saved 264 raw bytes cost 23 gzip. The gauge is the compressed download the browser fetches, not the cleaner source.

You don’t pass a board because the schematic says the filter should be quiet.

You hook the analyzer to the output and you read the marker. If the spur is still there, the drawing was wrong, or the build was. The measurement is the output.

Agent loops that optimize source and skip the artifact get this backwards.

Why did extracting a helper make Clarity’s script bigger?

On October 29, Microsoft Clarity’s Manu Nair wrote up how they made clarity.js smaller. The script runs on a lot of pages, so size is the spec. A lighter script is less JavaScript for the browser to download on the pages Clarity already runs on. They turned the work into a loop: an agent (they call it pi) made a change, built the script, measured the compressed output, ran the tests, and kept or reverted on the result.

After more than 50 experiments, clarity.js was 1,257 gzip bytes smaller. That’s a 4.5% cut, and they didn’t have to strip features to get it.

The interesting part is what the loop refused to trust.

Gzip finds repeated byte sequences and replaces them with short references. Repetitive source is almost free once gzip has seen the pattern. A new function name, a new code pattern, a unique string: that is a fresh dictionary entry, and it costs bytes on the wire.

They had four identical coordinate-tracking blocks, six lines each, copy-pasted across switch cases. They extracted a coordinates() helper. Raw size dropped 264 bytes. Gzipped size went up 23.

Gzip had already paid for the repetition. The helper added a new name, a new signature, and four new call sites. Fresh dictionary entries. Cleaner schematic. Worse output.

They saw the same pattern on a dim() helper (+13 gzip), hoisted arrays (+2), and ||= (+3). The last one is shorter syntax. It’s also a rare byte pattern. The old x = x || defaultValue was already in the dictionary.

If they had graded the agent on raw size, those land as wins. The download is what the customer fetches. That’s the output.

What should an agent optimize if the goal is a smaller download?

This is the same lesson as one run is an anecdote, pointed at a different gauge. Don’t report the proxy. Report the thing you actually ship.

Each build-measure cycle took about 35 seconds. That is cheap enough to be honest. You can throw away a “cleanup” that grew the file and keep moving. Nair’s point: five-byte improvements are not worth a human’s afternoon, and they are worth 35 seconds of automated experimentation. The agent also tried things a person might not have bothered with, including systematically testing about 15 Terser options, and finding that ||= made gzip worse.

What actually moved the gzip number, from their writeup:

  • Namespace import elimination, -444 gzip, about 35% of the total savings. import * as module used as a value forced Rollup to emit runtime namespace objects. Named imports let Rollup inline the calls. One file with 13 namespace imports in a dispatch table accounted for 324 of those bytes. Property-only import * was fine.
  • Removing entire functions, -84 gzip. A 12-line manual ReadableStream reader became one Response plus arrayBuffer call (-44). A parseSignals() wrapper that only called JSON.parse inside a try/catch the caller already had was another 20.
  • Terser options, -301 gzip. They tested compress and mangle flags one at a time. passes: 3, hoist_vars: true, and freeze: false on Rollup IIFE outputs helped. Property mangling collided with DOM names. Most other flags did nothing.
  • Template literals to concatenation, -110 gzip. Their target is ES5, so `${a}${b}` compiled into .concat calls. Writing a + b compiled to a+b.
  • Large block deduplication, -109 gzip. Small helpers hurt. A 27-line property copy in baseline.ts became Object.assign({}, buffer). Blocks of 20-plus unique lines are a different gzip problem than four identical six-line blocks.

Keep-or-revert on gzip is the whole loop. A cleaner diff that misses the number is a revert. Same as a board that matches the drawing and fails the test.

Your tests are lying to your agent is the cousin on the other gauge: if the suite is noisy, the keep-or-revert bit is lying too. Clarity ran the tests after every experiment. A smaller file that fails the suite is not a smaller file. It’s a fail.

What win could they not ship?

They also found a bigger win they could not ship. Bumping the TypeScript target off ES5, plus later syntax, saved 2,308 gzip bytes. Almost double everything else. The change removed the __awaiter / __generator polyfills TypeScript injects so async/await works in ES5, plus ES2020 optional chaining and nullish coalescing.

Clarity runs on Angular sites with Zone.js, which patches Promise and expects the old generator-based async output. Native async/await slips past that patch. Change detection breaks silently.

So the loop had a second output to measure: does the script still work on the sites you already support. A smaller file that bricks Angular is not a smaller file. It’s a fail.

That is the schematic-versus-output problem at the next layer. The TypeScript target is a drawing of a modern runtime. The installed base is the artifact. Compatibility is part of the spec.

What belongs on the traveler?

If you let an agent chase size, or cost, or latency:

  • Name the output. Gzipped bytes. Task completion. p95, not the pretty intermediate. Clarity named gzip because that is what the browser fetches.
  • Keep or revert on that number. A cleaner diff that misses the number is a revert. Same as a board that matches the drawing and fails the test.
  • Don’t ship a win the fixture can’t run. Compatibility is part of the spec. Clarity’s 2,308-byte target change is the example. The number was real. The unit could not leave the station.
  • Make the cycle cheap enough to throw work away. 35 seconds per experiment is why they could discard the helper that “saved” 264 raw bytes.

A worked example from their printed deltas:

  1. Helper extraction: -264 raw, +23 gzip. Revert.
  2. dim() helper: +13 gzip. Revert.
  3. Hoisted arrays: +2 gzip. Revert.
  4. ||=: +3 gzip. Revert.
  5. Named imports: -444 gzip. Keep.
  6. Function removal: -84 gzip. Keep.
  7. Terser flags that actually moved the needle: -301 gzip. Keep.
  8. ES2017 target: -2,308 gzip, breaks Zone.js. Do not ship.

If your agent report leads with raw line count, you graded the schematic.

This is also the other side of every call passed and nothing got done. A step can look like progress in the source diff and still move the real gauge the wrong way. Pass/fail has to be the output, or the loop will keep the wrong wins.

Measure at the output. The schematic is a hypothesis.

Source: How We Made clarity.js 4.5% Smaller Without Sacrificing Compatibility, Manu Nair, Microsoft Clarity, Oct 29, 2026

Frequently asked questions

Why did extracting a helper make Clarity's script bigger?

The coordinates() helper dropped 264 bytes of raw source. Gzipped size went up 23 bytes. Gzip already compressed the four identical blocks. The new function name, signature, and call sites were fresh dictionary entries, so the download got worse.

What should an agent optimize if the goal is a smaller JavaScript download?

The gzipped output, which is what the browser fetches. Clarity’s loop built the script, measured compressed size, ran the tests, and kept or reverted on that number. Raw line count and cleaner source are the schematic, not the measurement.

How much did Clarity actually ship?

After more than 50 experiments, clarity.js was 1,257 gzip bytes smaller, a 4.5% cut, with no cut in functionality. A newer TypeScript target would have saved another 2,308 gzip bytes. They could not ship it: Zone.js on Angular sites expects the old ES5 async output, and native async/await breaks change detection.

What cleanups made gzip worse besides the helper?

A dim() helper added 13 gzip bytes. Hoisted arrays added 2. The shorter ||= operator added 3, because it is a rare byte pattern while x = x || defaultValue was already in the dictionary.

Disagree or have a war story? Reply on X