Menu

Control Flow Obfuscator

Confuse JavaScript code structure with dead code injection, opaque predicates, string arrays, and expression encoding. AST-based — behavior stays the same, structure becomes unreadable.

Ready
Intensity 2
How this works: Parses JavaScript with Acorn, applies selected transforms to the AST, then generates code with Astring. Every transform preserves runtime behavior — but always test the output.
Realistic limits: This is not full control-flow flattening (the "switch state machine" approach that tools like javascript-obfuscator use). It applies safe, well-understood transforms. Heavier techniques risk breaking code.
Original JavaScript 0 chars
Obfuscated Output 0 chars
Understanding Control Flow Obfuscation

What Is Control Flow Obfuscation?

Control flow obfuscation is the practice of transforming a program's logical structure so it becomes hard to follow, without changing what the program does. Where identifier renaming changes names, control flow obfuscation changes structure — the shape of the code, the order of statements, the flow of execution.

The most aggressive form is control flow flattening: every block of code becomes a case in a giant switch statement, with a state variable controlling which case runs next. This is what tools like javascript-obfuscator (the popular npm package) do, and it turns a simple 20-line function into 100+ lines of unreadable dispatcher logic.

That technique is risky and hard to implement correctly. A single off-by-one error breaks the program silently. This tool takes a different approach: it applies safe, proven transforms that confuse structure without touching semantics.

The Transforms This Tool Applies

1. Dead Code Injection

Inserts code blocks that can never execute. The parser must still process them, and a human reader must still read them, but the runtime skips them. Example:

// Before
let x = 5;

// After
if (typeof __never_used === "undefined") {
  let fake1 = Math.random() * 1000;
  let fake2 = fake1 + "ghost";
} else {
  console.log("never runs");
}
let x = 5;

Dead code is cheap to inject and always safe. The only cost is a few bytes and a small runtime check (which is optimized away by the JIT).

2. Opaque Predicates

Wraps statements in conditions that are always true but look like real logic. Example:

// Before
doSomething();

// After
if ((1 + 1) === 2) {
  doSomething();
}

A human reader sees an if statement and has to think about it. A machine just sees "condition true, run body." This is the heart of most obfuscation: make the easy path look hard.

Stronger opaque predicates use properties like Math.sqrt(16) === 4 or []+[] === "" that look complex but are constant.

3. Number Encoding

Replaces literal numbers with equivalent expressions:

5        → (2 + 3)
42       → ((0x10 << 1) | 0x0A)
100      → (10 * 10)

At intensity 2, the tool uses two-term additions. At intensity 3, it uses bit operations that are harder to evaluate in your head.

4. Boolean Encoding

Replaces true and false with equivalent expressions:

true     → (!![])
false    → (![])

![] is false because an empty array is truthy, so ![] is false. !![] negates that to true. This is one of the oldest tricks in the JavaScript obfuscator playbook.

5. String Array

Collects all string literals into a single array at the top of the file, then replaces each usage with an array access:

// Before
console.log("Hello");
console.log("World");

// After
var _str = ["Hello", "World"];
console.log(_str[0]);
console.log(_str[1]);

This centralizes strings, making it harder to search the code for meaningful text. More advanced obfuscators shuffle the array and use accessor functions to decode it at runtime.

6. Identifier Renaming

Same as the standalone renamer tool: replaces variables, functions, and parameters with gibberish names. Combined with the above transforms, this produces code that is thoroughly unreadable.

Intensity Levels

LevelWhat It DoesBest For
1 — LightSimple additions, basic opaque predicatesProduction code that must stay fast
2 — MediumMixed operations, more dead codeDefault — good balance
3 — HeavyBit operations, maximum dead codeMaximum confusion (slower)

What This Tool Cannot Do

  • It cannot do full control flow flattening. The switch-based state machine technique is too risky without extensive testing — a single bug breaks the program silently.
  • It cannot encrypt your code. The output is still JavaScript. A determined reader can trace the logic, just slowly.
  • It cannot hide strings permanently. Strings are still visible, just centralized.
  • It cannot replace a real obfuscator. For production protection, use javascript-obfuscator with its full feature set.
  • It cannot guarantee behavior. Always test after obfuscation.

Best Practices

  • Start at intensity 1. Test thoroughly. Then move to 2 or 3.
  • Combine with minifier. Run control flow obfuscation, then minify the output for smaller files.
  • Combine with renaming. Enable "Rename Identifiers" for maximum effect.
  • Test edge cases. Async functions, generators, destructuring — these can interact with transforms in surprising ways.
  • Keep the original. Never lose the un-obfuscated source. If you find a bug, you will need it.

Frequently Asked Questions

Does this tool execute my code?
No. It parses your code, transforms the AST, and generates new code. The code is never run.
Will obfuscated code still work?
In most cases yes. But always test. Some transforms interact unexpectedly with async functions, generators, and destructuring.
Is my code uploaded to your server?
No. Everything runs in your browser using Acorn and Astring. Your code never leaves your device.
Why no full control flow flattening?
Because it's genuinely hard to do safely. One bug breaks the program silently. The popular javascript-obfuscator does it well, but it's thousands of lines of carefully tested code. This tool focuses on safe transforms.
Does it work with TypeScript?
Not directly — TypeScript must be compiled to JavaScript first. Use tsc or esbuild to transpile, then run this tool on the resulting JS.
Does it slow down the code?
Slightly. Dead code checks and opaque predicates add a small runtime cost. At intensity 3, some number encodings are slower. Modern JIT compilers often optimize most of this away.

Related Tools

External Resources