Skip to content

Benchmarking different FizzBuzz implementations in JavaScript

License

Notifications You must be signed in to change notification settings

ajdnik/fizzbuzz-js

Repository files navigation

FizzBuzz Benchmark

Benchmark

Benchmarking different FizzBuzz implementations in JavaScript (and Rust via napi-rs) to compare performance characteristics of various optimization strategies.

Implementations

# Implementation Description
1 Naive Classic approach using string concatenation
2 Least Common Multiple Uses modulo checks for 3, 5, and 15
3 Optimized Modulo Nested conditionals to minimize modulo operations
4 Array Pre-allocation Pre-allocated array with optimized modulo checks
5 Loop Unrolling Loop unrolling exploiting the 15-iteration FizzBuzz cycle
6 Tail Recursion + Trampolining Avoids stack overflow with trampoline pattern
7 Domain-Specific Language Composable chain of divisibility checks using continuation passing
8 Rust napi-rs Native Rust addon returning a JS array via N-API
9 Rust napi-rs (JSON.parse) Native Rust addon returning JSON, parsed on the JS side

Requirements

  • Node.js 20 or later
  • Rust toolchain (for building the napi-rs addon)

Usage

Install dependencies:

npm install

Build the Rust napi addon:

npm run build:rust

Run the benchmark:

npm run benchmark

Run the JavaScript tests:

npm test

Run the Rust tests:

npm run test:rust

Profiling

You can profile individual JavaScript FizzBuzz implementations using V8's optimization tracing flags. This is useful for inspecting which functions get optimized or deoptimized by the V8 JIT compiler.

npm run profile:opt <functionName>

Available functions: fizzBuzzNaive, fizzBuzzLCM, fizzBuzzModulo, fizzBuzzPreallocated, fizzBuzzUnrolled, fizzBuzzRecursive, fizzBuzzDSL.

The script runs the selected function 100,000 times with N=10,000 while --trace-opt and --trace-deopt print V8's optimization and deoptimization decisions to stderr.

CI

This project includes a GitHub Actions workflow that runs benchmarks across multiple runtimes:

  • Node.js — versions 20, 22, 24, 25
  • Bun — latest
  • Deno — latest

Results are collected and displayed in the Actions summary page.

About

Benchmarking different FizzBuzz implementations in JavaScript

Topics

Resources

License

Stars

Watchers

Forks