In the previous chapter, Performance-Aware Token Access, we learned how to make our authentication checks fast using caching so the application doesn't stutter.
Now, we face a final challenge: Weight.
Even if a feature is disabled, the code for it usually sits inside the application file that users download. This makes the app larger and slower to download. In this final chapter, we will learn how to make that code completely vanish when it isn't needed.
Imagine you are packing a suitcase for a summer vacation to Hawaii. You have a heavy winter coat in your closet.
In software, "leaving the coat at home" is called Dead Code Elimination (or Tree Shaking).
We use a special function provided by our build tool (Bun) called feature.
When we build the app, we tell the compiler: VOICE_MODE = false.
The compiler looks through our code. Everywhere it sees a check for that feature, it literally deletes the code inside that block before creating the final file.
We wrap our logic in a check using feature('FLAG_NAME').
import { feature } from 'bun:bundle';
if (feature('VOICE_MODE')) {
// This code only exists in the final app
// if we built it with VOICE_MODE turned on.
console.log("Voice systems online.");
}
What happens here:
VOICE_MODE is true: The code stays exactly as you wrote it.VOICE_MODE is false: The compiler deletes lines 3-6. The final app doesn't even know "Voice systems" existed.This process happens before the user ever downloads the app. It happens on your computer (or the build server) when you run the command to create the executable.
Let's visualize the "Tree Shaking" process. Imagine our code is a tree. The compiler shakes the tree, and any branch that is "dead" (disabled) falls off.
Let's look at voiceModeEnabled.ts one last time. We use this technique in our GrowthBook check.
We use a specific coding style here to ensure everything gets deleted properly.
import { feature } from 'bun:bundle'
export function isVoiceGrowthBookEnabled(): boolean {
// We use the ternary operator (? :)
return feature('VOICE_MODE')
? !getFeatureValue_CACHED('tengu_amber_quartz_disabled', false)
: false
}
Why do we write it this way?
If feature('VOICE_MODE') is false, the compiler sees this:
// The compiler simplifies the code to just this:
return false;
Because the first part was false, the compiler knows the second part (checking tengu_amber_quartz_disabled) can never happen.
This is powerful because it also removes Strings.
The string 'tengu_amber_quartz_disabled' takes up space. If we use the code above and disable Voice Mode, that string is deleted from the final binary. Hackers won't even be able to find that text in the app's memory because it simply isn't there.
Let's look at how this fits into the bigger picture we learned in Composite Readiness Logic.
// 1. If Build Flag is OFF -> Returns false immediately (Code deleted).
// 2. If Build Flag is ON -> Runs the function.
// 3. The function checks the Remote Kill Switch.
const isGo = isVoiceGrowthBookEnabled();
Congratulations! You have completed the tutorial on the Voice project's readiness architecture.
Let's review what you have built:
isVoiceModeEnabled) so the UI doesn't have to guess.You now have a robust, secure, fast, and lightweight system for managing features. Happy coding!
Generated by Code IQ