Skip to content

Instantly share code, notes, and snippets.

@mrousavy
Last active February 15, 2025 19:22
Show Gist options
  • Save mrousavy/8b2df68d941ce19480174a0f6a114a1b to your computer and use it in GitHub Desktop.
Save mrousavy/8b2df68d941ce19480174a0f6a114a1b to your computer and use it in GitHub Desktop.
Static Hermes benchmark
# 1. Build Hermes off of static_h branch.
# 2. Run these commands:
# Static Hermes
build/bin/shermes -strict -parse-ts -finline -O -typed main.ts
./a.out
# BENCHMARK: 50000 iterations took 12818.000ms
# Hermes
build/bin/hermes -O -finline -parse-ts main.ts
# BENCHMARK: 50000 iterations took 52101.000ms
# Bun
bun main.ts
# BENCHMARK: 50000 iterations took 7758.000ms
# When using 50 iterations instead of 50.000 iterations, all runtimes roughly execute at the same time: ~20ms
// Recursive quicksort implementation for numbers.
function quicksort(arr: number[]): number[] {
if (arr.length <= 1) return arr;
const pivot: number = arr[Math.floor(arr.length / 2)];
const left: number[] = [];
const middle: number[] = [];
const right: number[] = [];
// Partition the array into three subarrays.
for (const num of arr) {
if (num < pivot) {
left.push(num);
} else if (num > pivot) {
right.push(num);
} else {
middle.push(num);
}
}
// Recursively sort left and right partitions.
return [...quicksort(left), ...middle, ...quicksort(right)];
}
// Utility function to generate an array of random numbers.
function generateRandomArray(length: number): number[] {
const arr: number[] = [];
for (let i = 0; i < length; i++) {
arr.push(Math.random());
}
return arr;
}
// Define benchmark parameters.
const ARRAY_LENGTH: number = 1000;
const ITERATIONS: number = 50000;
// Run the benchmark: generate a random array and sort it with quicksort.
const start = new Date()
let result: number[] = [];
for (let i = 0; i < ITERATIONS; i++) {
const arr = generateRandomArray(ARRAY_LENGTH);
result = quicksort(arr);
}
const end = new Date()
console.log(`BENCHMARK: ${ITERATIONS} iterations took ${(end - start).toFixed(3)}ms`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment