Overview
Porffor can compile JavaScript directly to native binaries that run without any runtime dependencies. This is accomplished through 2c, Porffor’s own WebAssembly-to-C compiler, which generates optimized C code that’s then compiled to machine code.How It Works
The native compilation pipeline:- Porffor compiles JavaScript to WebAssembly bytecode
- 2c converts Wasm to optimized C source code
- C compiler produces the final native executable
Basic Usage
Compiler Selection
Porffor supports multiple C compilers. Choose with the--compiler flag:
Clang (Default)
GCC
Zig
The specified compiler must be installed and available in your PATH.
Optimization Levels
Control C compiler optimization with the--cO flag:
Ofast (Default)
- All O3 optimizations
- Disregards strict standards compliance
- Best performance for most code
O3
- Inline functions
- Loop vectorization
- Advanced interprocedural optimization
O2
- Good performance improvements
- Reasonable compilation time
- Safe for most code
O1
- Minimal performance improvements
- Fast compilation
- Good for debugging
O0
- Fastest compilation
- Easiest debugging
- Predictable code generation
Combining Optimizations
For maximum performance, combine Porffor and C compiler optimizations:- Optimizes at the Wasm level with Porffor’s
-O2 - Generates optimized C code via 2c
- Compiles with Clang’s
-Ofastfor maximum native performance
Real-World Examples
Example: Performance Benchmark
benchmark.js
Example: Command-Line Tool
greet.js
Advanced Features
Profile-Guided Optimization (PGO)
PGO uses runtime profiling to guide optimization:Cyclone Optimizer
Cyclone performs partial constant evaluation at the Wasm level:-O2:
- Constant folding
- Dead code elimination
- Type-based optimizations
- Loop simplification
Binary Stripping
Output binaries are stripped by default (symbols removed). To keep symbols for debugging:-d flag:
- Preserves debug symbols
- Includes function names
- Enables better debugger support
Compiling to C Source
You can also generate C source code without compiling to native:- Inspecting generated C code
- Custom compilation pipelines
- Cross-compilation workflows
- Understanding 2c’s code generation
2c Compiler Details
What is 2c?
2c is Porffor’s WebAssembly-to-C compiler. Unlikewasm2c and similar tools, 2c:
- Generates specific, optimized C code (not generic translation)
- Uses Porffor’s internal type information
- Produces minimal boilerplate
- Targets CLI binaries specifically
- Requires no external runtime files
Generated C Code Structure
The generated C includes:Performance Characteristics
Native vs Wasm
Native binaries typically:- Start faster: No Wasm instantiation overhead
- Run faster: Direct machine code execution
- Use less memory: No engine overhead
- Have smaller size: Stripped binaries are compact
Benchmarking
For compute-intensive tasks, native compilation can be 2-10x faster than interpreted JavaScript and competitive with JIT-compiled JavaScript after warmup.Platform Support
Native compilation works on:- Linux: x86_64, ARM64
- macOS: x86_64, ARM64 (Apple Silicon)
- Windows: x86_64
Troubleshooting
Compiler not found error
Compiler not found error
Install the required C compiler:Ubuntu/Debian:macOS:Windows:
Install Visual Studio Build Tools or LLVM/Clang for Windows.
Binary crashes or produces wrong output
Binary crashes or produces wrong output
Try lower optimization levels:Enable debug mode:Check for unsupported features:
- Complex async patterns
- Dynamic eval/Function
- Scope-crossing variables
Very slow compilation
Very slow compilation
High optimization levels can be slow:Or disable Porffor optimizations:
Large binary size
Large binary size
Ensure stripping is enabled (default):Use optimization to reduce code size:Avoid creating many objects and large data structures in global scope.
Next Steps
Optimization Strategies
Write code that compiles to faster binaries
Debugging
Debug native binaries and C code
WebAssembly Output
Compile to Wasm instead of native
TypeScript Support
Use TypeScript for better type safety