1. Home
  2. /
  3. Zig-wasm
  4. /
  5. Zig WASM Factorial

Zig WASM Factorial - Zig-wasm Typing CST Test

Loading…

Zig WASM Factorial — Zig-wasm Code

Calculates factorial recursively and prints the result.

# zig/demo/factorial.zig
const std = @import("std");

fn factorial(n: i32) i32 {
	if (n <= 1) return 1;
	return n * factorial(n - 1);
}

pub export fn factorial(n: i32) void {
	std.debug.print("Factorial: {d}\n", .{factorial(n)});
}

Zig-wasm Language Guide

Zig is a general-purpose programming language designed for robustness, optimality, and simplicity. With Zig-Wasm, developers can compile Zig code to WebAssembly, enabling high-performance, low-level applications in the browser or other Wasm runtimes.

Primary Use Cases

  • ▸Porting system-level libraries to WebAssembly
  • ▸High-performance game engines or simulations in the browser
  • ▸Cryptography, compression, or other CPU-intensive algorithms
  • ▸Replacing C/C++ Wasm modules with safer, simpler Zig code
  • ▸Low-level WASI (WebAssembly System Interface) applications

Notable Features

  • ▸Direct compilation to WebAssembly (Wasm32 target)
  • ▸No hidden runtime or garbage collector
  • ▸C interop for leveraging existing code
  • ▸Manual memory management with safety options
  • ▸Simple, readable, and maintainable syntax

Origin & Creator

Zig is created by Andrew Kelley, with the community contributing to language development, compiler improvements, and WebAssembly support.

Industrial Note

Zig-Wasm is particularly useful for developers who need predictable, high-performance WebAssembly modules, low-level memory control, or integration with C/C++ codebases for web execution.

Quick Explain

  • ▸Zig allows direct compilation to WebAssembly for running low-level code in browsers or servers.
  • ▸Focuses on safety, performance, and predictable behavior without a garbage collector.
  • ▸Supports manual memory management and fine-grained control over execution.
  • ▸Provides interoperability with C and other languages, making it versatile for Wasm projects.
  • ▸Ideal for system-level code, games, and performance-critical web applications.

Core Features

  • ▸Compile-time code execution for optimization
  • ▸Error handling without exceptions
  • ▸Explicit memory control for performance-critical modules
  • ▸Cross-compilation support including Wasm targets
  • ▸Lightweight standard library suitable for Wasm

Learning Path

  • ▸Learn Zig language fundamentals
  • ▸Understand WebAssembly basics
  • ▸Compile Zig modules to wasm32 target
  • ▸Integrate Wasm module with JavaScript frontend
  • ▸Optimize and debug Zig-Wasm for performance

Practical Examples

  • ▸Math library compiled to Wasm for frontend use
  • ▸Audio or image processing in browser via Zig-Wasm
  • ▸Cryptography routines executed securely in Wasm
  • ▸Game physics engine modules
  • ▸Compression/decompression utilities for web apps

Comparisons

  • ▸Zig-Wasm vs Rust-Wasm: Zig is lower-level, simpler, no borrow checker; Rust has stronger type safety and ecosystem
  • ▸Zig-Wasm vs C/C++-Wasm: Zig safer syntax, modern compiler, easier cross-compilation
  • ▸Zig-Wasm vs AssemblyScript: Zig offers manual memory control and system-level features; AssemblyScript is TS-like with GC
  • ▸Zig-Wasm vs Go-Wasm: Zig has smaller runtime and more predictable memory behavior
  • ▸Zig-Wasm vs TinyGo: Zig focuses on performance and low-level control, TinyGo targets small Go programs to Wasm

Strengths

  • ▸Predictable and deterministic performance
  • ▸Safe alternative to C for low-level WebAssembly
  • ▸Small runtime footprint, ideal for Wasm
  • ▸Easier debugging than C/C++ in Wasm
  • ▸High interoperability with other languages and platforms

Limitations

  • ▸Smaller ecosystem compared to Rust or JS frameworks
  • ▸No built-in reactive or UI framework
  • ▸Requires manual memory and resource management
  • ▸Limited high-level abstractions for web development
  • ▸Debugging in browser WebAssembly can be challenging

When NOT to Use

  • ▸Applications requiring high-level web frameworks
  • ▸Projects needing large JavaScript ecosystem libraries
  • ▸Rapid prototyping with minimal low-level code
  • ▸Teams unfamiliar with manual memory management
  • ▸UI-heavy projects needing reactive frameworks

Cheat Sheet

  • ▸zig init-exe -> create new Zig executable project
  • ▸zig build-exe src/main.zig -target wasm32-freestanding -> compile to Wasm
  • ▸zig test -> run unit tests
  • ▸zig fmt -> format code
  • ▸zig run src/main.zig -> run locally for testing

FAQ

  • ▸Is Zig-Wasm free?
  • ▸Yes - Zig is open-source and free
  • ▸Does it require a runtime?
  • ▸No, compiled Wasm runs in browser or Wasm runtime
  • ▸Can I use Zig for UI frameworks?
  • ▸Not directly - Zig-Wasm is low-level, use JS glue for frontend
  • ▸Is memory managed automatically?
  • ▸No, Zig uses manual memory management with optional safety checks
  • ▸Does Zig interoperate with C?
  • ▸Yes - full C interoperability for libraries or legacy code

30-Day Skill Plan

  • ▸Week 1: Zig syntax, pointers, slices, error unions
  • ▸Week 2: Compile-time execution and memory management
  • ▸Week 3: Exporting functions to Wasm and JS interop
  • ▸Week 4: Optimize binary size and execution speed
  • ▸Week 5: Integrate with WASI, JS frameworks, or C libraries

Final Summary

  • ▸Zig-Wasm enables low-level, high-performance WebAssembly modules.
  • ▸Ideal for system-level, CPU-intensive, or deterministic tasks in the browser.
  • ▸No garbage collector, manual memory control, small runtime footprint.
  • ▸Seamlessly interoperates with JS, C, and WASI runtimes.
  • ▸Perfect for developers seeking predictable, efficient, and safe WebAssembly applications.

Project Structure

  • ▸src/ - Zig source files
  • ▸build.zig - optional build script
  • ▸out/ - compiled WebAssembly output
  • ▸tests/ - unit tests for Zig modules
  • ▸examples/ - sample usage of Zig-Wasm modules

Monetization

  • ▸Deliver high-performance web apps
  • ▸Reduce backend computation via client-side Wasm
  • ▸Port legacy C libraries safely to browser
  • ▸Optimize Wasm modules for size-sensitive projects
  • ▸Enable high-speed computation in SaaS products

Productivity Tips

  • ▸Use build.zig scripts for repeatable builds
  • ▸Write small, composable modules
  • ▸Profile Wasm binaries for size and speed
  • ▸Leverage compile-time execution for constants
  • ▸Reuse code across Zig projects

Basic Concepts

  • ▸Compile-time execution - code runs at compile time for optimization
  • ▸Manual memory management - allocate/free memory explicitly
  • ▸Error unions - lightweight error handling mechanism
  • ▸Slices and pointers - low-level data structures for Wasm
  • ▸Exported functions - callable from JavaScript

Official Docs

  • ▸https://ziglang.org/documentation/master/
  • ▸https://ziglang.org/learn/

More Zig-wasm Typing Exercises

Simple Zig WebAssembly ProgramZig WASM Add Two NumbersZig WASM Multiply Two NumbersZig WASM FibonacciZig WASM Even CheckZig WASM Maximum of Two NumbersZig WASM Minimum of Two NumbersZig WASM Toggle BooleanZig WASM Print Array

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher