1. Home
  2. /
  3. Assemblyscript
  4. /
  5. Fibonacci Function

Fibonacci Function - Assemblyscript Typing CST Test

Loading…

Fibonacci Function — Assemblyscript Code

Calculates Fibonacci number recursively.

# assemblyscript/demo/fibonacci.ts
export function fibonacci(n: i32): i32 {
	if(n <= 1) return n;
	return fibonacci(n - 1) + fibonacci(n - 2);
}

Assemblyscript Language Guide

AssemblyScript is a TypeScript-like language that compiles to WebAssembly (Wasm). It allows developers familiar with TypeScript/JavaScript to write high-performance WebAssembly modules for web, server, and blockchain applications.

Primary Use Cases

  • ▸High-performance web modules
  • ▸Blockchain smart contracts (e.g., NEAR Protocol, Polkadot parachains)
  • ▸Game engines or physics simulations in the browser
  • ▸Data processing in the browser or edge environments
  • ▸Embedding Wasm modules in Node.js or serverless platforms

Notable Features

  • ▸TypeScript-like syntax with optional static typing
  • ▸Direct compilation to WebAssembly
  • ▸Small and efficient Wasm output
  • ▸Supports import/export with JavaScript
  • ▸Active open-source ecosystem and tooling

Origin & Creator

AssemblyScript is an open-source project maintained by the AssemblyScript community, started around 2017.

Industrial Note

AssemblyScript is ideal for web developers seeking performance-critical modules, blockchain developers building smart contracts on Wasm chains, and projects that need a bridge between TypeScript and WebAssembly.

Quick Explain

  • ▸AssemblyScript uses a syntax similar to TypeScript, making it accessible to JS/TS developers.
  • ▸It compiles to WebAssembly, enabling near-native performance for computational tasks.
  • ▸Supports integration with JavaScript via WebAssembly imports and exports.
  • ▸Commonly used in web apps, games, blockchain smart contracts, and performance-critical modules.
  • ▸Enables leveraging type safety and static typing while targeting WebAssembly.

Core Features

  • ▸Compile `.ts` files to `.wasm` modules
  • ▸Memory management via linear memory and optional garbage collection
  • ▸Strongly typed with explicit numeric types
  • ▸Integration with npm tooling and TypeScript ecosystem
  • ▸Debugging and testing via Node.js or browser runtimes

Learning Path

  • ▸Learn TypeScript basics
  • ▸Understand WebAssembly concepts
  • ▸Install AssemblyScript and build simple modules
  • ▸Explore memory management and imports/exports
  • ▸Deploy WebAssembly in web or Node.js environments

Practical Examples

  • ▸Math-heavy computations in browser games
  • ▸Image or audio processing modules
  • ▸Smart contracts on NEAR Protocol
  • ▸Cryptographic functions compiled to Wasm
  • ▸Data transformations in edge computing

Comparisons

  • ▸AssemblyScript vs Rust: easier for TS devs vs full-featured system language
  • ▸AssemblyScript vs C++/Wasm: simpler syntax vs mature toolchain
  • ▸AssemblyScript vs TypeScript: TS compiles to JS vs AS compiles to Wasm
  • ▸AssemblyScript vs Go Wasm: lighter runtime vs Go standard library
  • ▸AssemblyScript vs Emscripten: native C/C++ to Wasm vs TS-like syntax

Strengths

  • ▸Leverages TypeScript knowledge for Wasm development
  • ▸Produces small, fast WebAssembly binaries
  • ▸Supports both client-side and server-side execution
  • ▸Works with existing JS code via imports/exports
  • ▸Enables blockchain contracts on Wasm-based chains

Limitations

  • ▸Not full TypeScript - some features (generics, classes) are limited
  • ▸Manual memory management may be required for advanced use
  • ▸Limited standard library compared to JS or Rust
  • ▸Debugging can be more complex than JS
  • ▸Tooling and ecosystem smaller than Rust/C++ for Wasm

When NOT to Use

  • ▸Projects needing complex OS-level operations
  • ▸Large-scale applications needing mature libraries
  • ▸Apps with advanced concurrency requirements
  • ▸Memory-intensive server programs beyond Wasm limits
  • ▸Developers unfamiliar with TypeScript or JS

Cheat Sheet

  • ▸npx asc index.ts -b index.wasm -> compile TS to Wasm
  • ▸export function name(...) -> exposes function to host
  • ▸import { foo } from 'env' -> import function from JS host
  • ▸as-pect -> test AssemblyScript modules
  • ▸memory.grow(n) -> increase linear memory pages

FAQ

  • ▸Can I use AssemblyScript for blockchain?
  • ▸Yes, NEAR Protocol and Substrate support AssemblyScript contracts.
  • ▸Is AssemblyScript the same as TypeScript?
  • ▸No, similar syntax but compiles to Wasm, with limited features.
  • ▸Does AssemblyScript support classes?
  • ▸Yes, with some limitations for performance.
  • ▸Can I use npm libraries?
  • ▸Only those compatible with AssemblyScript or JS interop.
  • ▸Is AssemblyScript faster than JS?
  • ▸Yes, it runs as WebAssembly with near-native performance.

30-Day Skill Plan

  • ▸Week 1: TypeScript and AssemblyScript syntax
  • ▸Week 2: Compile and run simple Wasm modules
  • ▸Week 3: Memory management and performance tuning
  • ▸Week 4: Integrate with JS and edge platforms
  • ▸Week 5: Advanced projects like blockchain contracts or games

Final Summary

  • ▸AssemblyScript enables TypeScript developers to write WebAssembly modules.
  • ▸Provides high performance for web, server, and blockchain applications.
  • ▸Strong typing, memory control, and Wasm compilation for efficiency.
  • ▸Ideal for numeric-heavy tasks, games, cryptography, and smart contracts.
  • ▸Bridges the gap between JS/TS ease-of-use and native performance.

Project Structure

  • ▸assembly/ - AssemblyScript source files
  • ▸build/ - compiled WebAssembly outputs
  • ▸asconfig.json - compiler and project config
  • ▸package.json - dependencies and scripts
  • ▸test/ - unit tests with as-pect or Jest

Monetization

  • ▸High-performance web modules for SaaS
  • ▸Blockchain smart contracts for dApps
  • ▸WebAssembly-based games or simulations
  • ▸Edge computing services
  • ▸Integration modules for paid web platforms

Productivity Tips

  • ▸Reuse AssemblyScript utility functions
  • ▸Batch operations to minimize JS-Wasm calls
  • ▸Keep memory footprint small for browser
  • ▸Use automated tests with as-pect
  • ▸Document host interop clearly

Basic Concepts

  • ▸Module - compiled WebAssembly output
  • ▸Export - function exposed to host (JS or runtime)
  • ▸Import - function or memory accessed from host
  • ▸Linear Memory - WebAssembly memory for data
  • ▸Primitive types - i32, i64, f32, f64, bool, etc.

Official Docs

  • ▸https://www.assemblyscript.org/
  • ▸https://www.assemblyscript.org/overview.html

More Assemblyscript Typing Exercises

Simple AssemblyScript FunctionSubtract Two NumbersMultiply Two NumbersDivide Two NumbersFactorial FunctionCheck Even NumberCheck Odd NumberMaximum of Two NumbersMinimum of Two Numbers

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher