Bytecode Compilation
QuickJS can compile JavaScript to bytecode for faster loading and to protect source code. This tutorial covers different bytecode compilation methods.Raw Bytecode Output
1
Create a JavaScript file
Create
hello.js:2
Compile to bytecode
Use the This creates a binary file containing the compiled bytecode.
-b flag to generate raw bytecode:3
Run the bytecode
QuickJS can execute bytecode files directly:Expected output:Bytecode files load faster than source files since they skip the parsing step.
Embedding Bytecode in C
1
Generate C code with bytecode
Use the This generates a C file containing:
-e flag to generate a complete C program:- Bytecode as a static array
- A
main()function that initializes QuickJS and executes the bytecode
2
Compile the C program
Compile with the QuickJS library:
3
Run the executable
Advanced Compilation Options
Strip Source Code
Remove source code from bytecode to reduce size and protect IP:-s twice to also strip debug information:
Set Stack Size
Limit the maximum stack size:Custom C Names
Set the C variable name for the bytecode:my_bytecode instead of the default.
Set Script Name
Customize the script name used in stack traces:Compiling Modules
ES6 Module
Compile a module to bytecode:-m flag tells qjsc to treat the file as an ES6 module.
Dynamic Module
Create a dynamically loadable module:-D flag generates code for a module that can be loaded with import().
External C Module
Add initialization code for an external C module:my_module with init function my_init.
Standalone Executables
QuickJS offers a simpler way to create standalone executables without manual C compilation:- Compiles JavaScript to bytecode
- Bundles bytecode into a copy of the
qjsexecutable - Same runtime dependencies as
qjs - No bundling (use
esbuildor similar for multi-file apps)
Debugging Bytecode
Dump bytecode for inspection:0x01- Final bytecode0x02- Pass 2 bytecode0x04- Pass 1 bytecode0x10- Bytecode in hex format0x20- Line number table
Performance Considerations
Bytecode advantages:- Faster loading (no parsing required)
- Smaller file size (when stripped)
- Source code protection
- Not human-readable
- Debugging is harder (especially when stripped)
- Must recompile for code changes
- Use bytecode for production deployments
- Keep source files for development
- Don’t strip debug info during development
- Test bytecode before deploying
Next Steps
- Learn about embedding QuickJS
- Explore native C functions
- Work with JavaScript modules