Skip to main content
Porffor supports multiple optimization levels controlled by the -O flag, similar to traditional compilers like GCC and Clang.

Optimization Levels

-O0: No Optimization

-O0
flag
Disable all optimizations.
porf -O0 script.js
When -O0 is set, the optimizer immediately returns without performing any transformations.Use cases:
  • Debugging compiled output
  • Fastest compilation times
  • Testing unoptimized code paths
Source reference: source/compiler/opt.js:8

-O1: Basic Optimization (Default)

-O1
flag
default:"true"
Enable basic optimizations. This is the default optimization level.
porf -O1 script.js
# or simply
porf script.js
Optimizations performed:
  • Peephole optimizations (local.get/local.set consolidation)
  • Constant folding
  • Dead code elimination (unused blocks)
  • Redundant conversion removal (i32→f64→i32, etc.)
  • Type conversion optimization
  • Instruction combining (local.set + local.get → local.tee)
Examples:
# Before optimization
local.set 0
local.get 0

# After -O1
local.tee 0
# Before optimization
local.get 0
drop

# After -O1
# (removed)
# Before optimization
i32.const 0
i32.eq

# After -O1
i32.eqz
Source reference: source/compiler/opt.js:1-248

-O2: Advanced Optimization

-O2
flag
Enable advanced optimizations. May be unstable.
porf -O2 script.js
Includes all -O1 optimizations plus:
  • Cyclone optimizer (partial constant evaluation)
  • More aggressive transformations
When -O2 is enabled, the Cyclone optimizer is automatically activated unless explicitly disabled.
-O2 enables experimental optimizations that may be unstable. Use with caution in production.
Source reference: source/compiler/index.js:88-91

-O3: Maximum Optimization

-O3
flag
Enable maximum optimizations (experimental).
porf -O3 script.js
Currently has similar behavior to -O2. Additional optimizations may be added in the future.Source reference: source/runtime/index.js:47

Cyclone Optimizer

The Cyclone optimizer is Porffor’s partial constant evaluator - it’s fast and powerful (hence the name “cyclone”).
--cyclone
flag
Explicitly enable or disable the Cyclone optimizer.
# Enable Cyclone (automatically enabled with -O2)
porf --cyclone script.js

# Disable Cyclone even with -O2
porf -O2 --no-cyclone script.js
Cyclone performs:
  • Constant propagation and folding
  • Dead local elimination
  • Stack-based partial evaluation
  • Control flow simplification
  • Type-based optimizations
Source reference: source/compiler/cyclone.js:1-4
--cyclone-log
flag
Log Cyclone optimizer performance and results.
porf --cyclone --cyclone-log script.js
Displays:
  • Per-function optimization results (ops before → after)
  • Total optimization time
  • Size difference in bytes
Source reference: source/compiler/index.js:132-147

Profile-Guided Optimization

--pgo
flag
Enable profile-guided optimization.
porf --pgo script.js
PGO runs the code to collect runtime profiling data, then uses that data to guide optimizations.Source reference: source/compiler/index.js:93
--pgo-log
flag
Log PGO performance and results.
porf --pgo --pgo-log script.js
Source reference: source/compiler/index.js:114-124

Optimization Configuration

Wasm Optimization Runs

--opt-wasm-runs
number
default:"2"
Number of optimization passes to run on each function.
porf --opt-wasm-runs=5 script.js
More runs can catch additional optimization opportunities but increase compilation time.Source reference: source/compiler/opt.js:21

Optimization Logging

--opt-log
flag
Log individual optimizations as they’re applied.
porf --opt-log script.js
Useful for debugging optimizer behavior and understanding what transformations are applied.Source reference: source/compiler/opt.js:55

Specific Optimizations

Unused Code Elimination

--opt-unused
boolean
default:"true"
Remove unused code and variables.
porf --no-opt-unused script.js

Fast Length

--fast-length
flag
Non-compliant optimization to make .length property access faster.
porf --fast-length script.js
This assumes .length always returns a number without runtime type checking. This breaks spec compliance but can provide performance benefits.Source reference: source/compiler/codegen.js:1887

Type Optimizations

--opt-types
boolean
default:"true"
Optimize based on type information.
porf --no-opt-types script.js
Enables type-guided optimizations throughout the compiler.
--rm-unused-types
boolean
default:"true"
Remove unused type checking code.
porf --no-rm-unused-types script.js

Comparison Table

Feature-O0-O1 (default)-O2-O3
Peephole optimizations
Constant folding
Dead code elimination
Cyclone optimizer
Compile timeFastestFastSlowerSlower
Output sizeLargestSmallerSmallestSmallest
StabilityStableStableExperimentalExperimental

Examples

Debug build (fast compilation)

porf -O0 -d script.js

Production build (balanced)

porf -O1 script.js output.wasm

Aggressive optimization

porf -O2 --pgo --wasm-opt script.js output.wasm

Maximum optimization with logging

porf -O2 --cyclone-log --pgo-log --opt-log script.js

See Also

Build docs developers (and LLMs) love