Create a custom JMH-style benchmark for TypeScript on Arm

This section demonstrates how to benchmark TypeScript functions using a JMH-style (Java Microbenchmark Harness) methodology implemented with Node.js’s built-in perf_hooks module. Unlike basic console.time() measurements, this approach executes multiple iterations, computes the average runtime, and produces stable and repeatable performance data, useful for evaluating workloads on your Google Cloud C4A (Axion Arm64) VM running SUSE Linux.

Implement benchmarking with Node.js perf_hooks on Arm

Create a file named benchmark_jmh.ts inside your project directory with the content below:

    

        
        
import { performance } from 'perf_hooks';

// Function to benchmark
const sumArray = (n: number) => {
    let sum = 0;
    for (let i = 0; i < n; i++) sum += i;
    return sum;
};

// Benchmark parameters
const iterations = 10;           // Number of repeated runs
const arraySize = 1_000_000;     // Size of array
let totalTime = 0;

// JMH-style repeated runs
for (let i = 0; i < iterations; i++) {
    const start = performance.now();
    sumArray(arraySize);
    const end = performance.now();
    const timeTaken = end - start;
    totalTime += timeTaken;
    console.log(`Iteration ${i + 1}: ${timeTaken.toFixed(3)} ms`);
}

// Compute average execution time
const averageTime = totalTime / iterations;
console.log(`\nAverage execution time over ${iterations} iterations: ${averageTime.toFixed(3)} ms`);

    

Code explanation:

ComponentDescription
performance.now()Provides high-resolution timestamps (sub-millisecond precision) for accurate timing.
sumArray(n)A simple CPU-bound function that sums integers from 0 to n. This simulates a computational workload suitable for benchmarking raw arithmetic throughput.
iterationsDefines how many times the test runs. Multiple repetitions reduce noise and help average out one-off delays or GC pauses.
Loop and averagingEach run’s duration is recorded; the mean execution time is then reported, mirroring how JMH computes stable results in Java microbenchmarks.

This JMH-style benchmarking approach provides more accurate and repeatable performance metrics than a single execution, making it ideal for performance testing on Arm-based systems.

Compile the TypeScript Benchmark

First, compile the benchmark file from TypeScript to JavaScript using the TypeScript compiler (tsc):

    

        
        
tsc benchmark_jmh.ts

    

This command transpiles your TypeScript code into standard JavaScript, generating a file named benchmark_jmh.js in the same directory. The resulting JavaScript can be executed by Node.js, allowing you to measure performance on your Google Cloud C4A (Arm64) virtual machine.

Run the benchmark

Now, execute the compiled JavaScript file with Node.js:

    

        
        
node benchmark_jmh.js

    

You should see output similar to:

    

        
        Iteration 1: 2.286 ms
Iteration 2: 0.749 ms
Iteration 3: 1.145 ms
Iteration 4: 0.674 ms
Iteration 5: 0.671 ms
Iteration 6: 0.671 ms
Iteration 7: 0.672 ms
Iteration 8: 0.667 ms
Iteration 9: 0.667 ms
Iteration 10: 0.673 ms

Average execution time over 10 iterations: 0.888 ms

        
    

Interpret your TypeScript performance data

Each iteration measures how long it takes to run the benchmarked function once, while the average execution time is calculated by dividing the total time for all runs by the number of iterations. Running the benchmark multiple times helps smooth out fluctuations caused by factors like CPU scheduling, garbage collection, or memory caching. This approach produces more consistent and meaningful performance data, similar to the methodology used by Java’s JMH benchmarking framework.

The average execution time reflects how efficiently the function executes under steady-state conditions. The first iteration often shows higher latency because Node.js performing initial JIT (Just-In-Time) compilation and optimization, a common warm-up behavior in JavaScript/TypeScript benchmarks.

Benchmark summary

Results from the earlier run on the c4a-standard-4 (4 vCPU, 16 GB memory) Arm64 VM in GCP (SUSE):

Iteration12345678910Average
Time (ms)2.2860.7491.1450.6740.6710.6710.6720.6670.6670.6730.888

Summarize TypeScript benchmarking results on Arm64

Here’s what the benchmark results show for Google Axion C4A Arm-based instances:

  • The average execution time on Arm64 is about 0.888 ms, which means TypeScript code runs efficiently on Arm-based VMs.
  • The first run is usually a bit slower because Node.js is warming up and optimizing the code. This is normal for all architectures.
  • After the first run, the times are very consistent, showing that Arm64 delivers stable performance for repeated tasks.

These results confirm that Google Cloud C4A Arm64 virtual machines are reliable and fast for running TypeScript workloads, whether you’re building application logic, scripts, or performance-sensitive services.

Back
Next