In a library's published output you find `const registry = /*#__PURE__*/ createRegistry();`. What does that comment assert, what reads it, and what is the risk of adding one?
answer
- an assertion, not an observation
- attaches to one call expression
- only fires when the result is unused
- placement is strict
- nothing re-checks it as code changes
basics
~20 sIt asserts that the immediately following call has no observable effects, so if its result is unused the whole call may be deleted. Minifiers and bundlers read it. Nothing verifies it, so a wrong annotation silently removes real behaviour from production builds.
solid answer
~50 s`/*#__PURE__*/` is an annotation convention understood by minifiers and bundlers — it originated in UglifyJS/Terser and is honoured by Rollup, esbuild and webpack. Placed immediately before a call or `new` expression, it asserts that evaluating that expression produces no observable side effect, so if nothing reads the result the tool may delete the whole call and, transitively, anything only that call referenced. It is the finest-grained escape hatch in the family: `sideEffects` in package.json speaks about whole files, this speaks about one expression. Compilers such as Babel emit them automatically for constructs they know are safe, and library authors add them by hand. The risk is that it is an unchecked promise. Annotate a call that actually registers something, mutates a global or throws, and the effect vanishes from optimised builds only — dev works, tests that keep the result pass, production quietly misbehaves.
code
javascript · 10 lines// Honest: buildFormatter only computes and returns; deleting the call is invisible
const formatter = /*#__PURE__*/ buildFormatter('en-GB');
// Ignored: the comment is not immediately before the callee expression
/*#__PURE__*/ const other = buildFormatter('de-DE');
// Dangerous: the call registers a plugin, so deleting it removes behaviour
const plugin = /*#__PURE__*/ registerPlugin('analytics');
export { formatter };go deeper
Know that this comment is a hint to build tools, not JavaScript syntax, and that it tells them a particular call is safe to delete if its result is never used.
Explain the two conditions for removal — unused result plus the annotation — and the strict placement rule, and name the coarser alternatives (sideEffects for files, restructuring for the real fix).
Show the risk analysis: it is an unverified promise, its failures appear only in optimised builds, and purity can regress silently after the annotation is written. Describe how you would verify it empirically on a real consumer build.
Own the policy: whether library authors in your org may annotate at all, what invariant is documented at the annotated function, and how a build-time smoke test on the optimised output protects downstream consumers from a stale assertion.
## What the annotation says A bundler will not delete `createRegistry()` on its own, even when nothing reads the result, because it cannot see whether the call touches shared state. The annotation supplies the missing fact: ```js const registry = /*#__PURE__*/ createRegistry(); ``` Read it as: *if you decide the result of this expression is unused, deleting the expression is safe.* Two conditions must both hold for removal — the result must be unreachable, and the annotation must be present. An annotated call whose result is used stays exactly where it is. The `/*@__PURE__*/` spelling is equivalent, and both `f()` and `new C()` forms are supported. Placement is strict: the comment must sit immediately before the callee expression. Put it on the previous line, before the `const`, or before an assignment rather than the call, and it is simply ignored — a silent no-op that people misread as "the annotation didn't help". ## Who writes them and who reads them Mostly you do not write them by hand in application code. They show up in *compiled* output: Babel emits them around helper calls and downlevelled class constructs it knows are pure, Rollup preserves and propagates them, and library build pipelines add them to factory calls so consumers can drop unused APIs. On the reading side, Terser was the original consumer, and Rollup, esbuild and webpack (via its minifier) all honour them. The reason libraries care disproportionately: a UI or utility library often exports dozens of objects built by factory calls at module scope. Without annotations, each of those calls is opaque, so importing one export keeps them all. With annotations, a consumer using one export drops the rest. ## Why the transitive part matters Deleting a call can unlock a cascade. If `createRegistry` was the only reference to an imported module, that import becomes unused and may go too, and so on up the graph. This is where the real size wins come from — a single annotated factory call can be holding an entire dependency subtree alive. ## The risk, concretely The annotation is a promise the toolchain trusts and never checks. Consider: ```js // This call is NOT pure — the annotation is a lie const plugin = /*#__PURE__*/ registerPlugin('analytics'); ``` If `plugin` is unused, optimised builds delete the registration. Development builds usually skip minification, so it works locally. Unit tests that assert on the return value keep the call reachable, so they pass. What ships is missing a plugin, and the runtime symptom appears far from the deleted line. A second, subtler risk: purity can regress. A call that was genuinely pure when annotated acquires a `console.warn`, a cache write or a metrics counter three releases later, and the stale annotation now licenses deleting real behaviour. Annotations are assertions about code that keeps changing, and nothing re-checks them. ## Related, coarser tools For whole functions rather than single call sites, Rollup and esbuild also understand a `/*#__NO_SIDE_EFFECTS__*/` annotation placed before a function declaration, which asserts that *every* call to that function is pure — so call sites need no individual annotation. It carries the same trust model and the same risk, scaled up. And at the file and package level, `sideEffects` in `package.json` is the coarsest instrument of the three. Pick the narrowest one that expresses what you actually know. ## How to use them responsibly - Prefer restructuring over annotating. A factory the consumer calls explicitly needs no promise about purity. - Annotate only calls whose implementation you own and can keep pure. - Verify by building a consumer that uses nothing from the annotated module and checking that the code is actually gone, then that the app still works. - Treat an annotation as documentation with teeth: a comment near the callee stating that it must stay pure, so the next person editing it knows the constraint exists.
- Does an annotated call get deleted even when its result is used?No. Two conditions must hold: the result must be unreachable from anything that survives, and the annotation must license removal. `const x = /*#__PURE__*/ f(); export { x };` with a consumer reading `x` keeps the call. The annotation only removes the tool's reason to be conservative; it never overrides reachability.
- Where exactly must the comment be placed?Immediately before the callee expression of the call or `new` it annotates — `/*#__PURE__*/ createRegistry()`, not before the `const` keyword and not on a preceding line separated from the expression. Misplaced annotations are silently ignored rather than reported, which is why an annotation that appears to do nothing is usually in the wrong position.
- How would you catch an annotation that has become a lie after the annotated function changed?There is no tool that verifies it, so it has to be process. Keep annotations only on functions you own, leave a comment at the function declaration stating that it must remain pure, and back it with a build check: produce a bundle from a consumer that uses none of the annotated exports, and run smoke tests against that optimised build rather than only against the development build.
saying these in an interview costs you the question
- Thinks the tool verifies the call really is pure
- Believes the annotation deletes the call unconditionally
- Puts the comment before the const instead of the call
- Assumes it is a language feature rather than a tool convention
- Ignores that the annotated function's purity can regress later