html
<!DOCTYPE html>
<html>
<head>
<title>WASM vs JS TEA Encryption Benchmark</title>
<script type="module">
import init, { tea_encrypt } from './pkg/wasm_tea.js';
// Native JS TEA implementation (identical algorithm for fair comparison)
function js_tea_encrypt(data, key) {
const k0 = ((key[0] ) | (key[1]<<8) | (key[2]<<16) | (key[3]<<24)) >>> 0;
const k1 = ((key[4] ) | (key[5]<<8) | (key[6]<<16) | (key[7]<<24)) >>> 0;
const k2 = ((key[8] ) | (key[9]<<8) | (key[10]<<16)| (key[11]<<24)) >>> 0;
const k3 = ((key[12] ) | (key[13]<<8)| (key[14]<<16)| (key[15]<<24)) >>> 0;
const delta = 0x9E3779B9 >>> 0;
for (let i = 0; i < data.length; i += 8) {
let v0 = ((data[i ] ) | (data[i+1]<<8) | (data[i+2]<<16) | (data[i+3]<<24)) >>> 0;
let v1 = ((data[i+4] ) | (data[i+5]<<8) | (data[i+6]<<16) | (data[i+7]<<24)) >>> 0;
let sum = 0;
for (let r = 0; r < 32; r++) {
sum = (sum + delta) >>> 0;
v0 = (v0 + ((((v1 << 4) >>> 0) + k0) ^ (v1 + sum) ^ (((v1 >>> 5) >>> 0) + k1))) >>> 0;
v1 = (v1 + ((((v0 << 4) >>> 0) + k2) ^ (v0 + sum) ^ (((v0 >>> 5) >>> 0) + k3))) >>> 0;
}
data[i ] = v0 & 0xFF;
data[i+1] = (v0 >>> 8) & 0xFF;
data[i+2] = (v0 >>>16) & 0xFF;
data[i+3] = (v0 >>>24) & 0xFF;
data[i+4] = v1 & 0xFF;
data[i+5] = (v1 >>> 8) & 0xFF;
data[i+6] = (v1 >>>16) & 0xFF;
data[i+7] = (v1 >>>24) & 0xFF;
}
}
async function runBenchmark() {
await init();
const SIZE = 10 * 1024 * 1024; // 10 MB buffer (must be multiple of 8)
const key = new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
// Create test buffer (random-ish data, multiple of 8)
const input = new Uint8Array(SIZE);
for (let i = 0; i < SIZE; i++) input[i] = (i * 7) % 256;
// Warm-up runs
console.log('Warm-up...');
const buf1 = input.slice();
js_tea_encrypt(buf1, key);
const buf2 = input.slice();
tea_encrypt(buf2, key);
// Verify correctness
if (!buf1.every((v, i) => v === buf2[i])) {
throw new Error('WASM and JS outputs differ!');
}
console.log('✅ Correctness verified');
// Benchmark JS (5 runs)
let jsTotal = 0;
console.group('JS Benchmarks (10MB)');
for (let run = 0; run < 5; run++) {
const buf = input.slice();
console.time(`JS run ${run + 1}`);
js_tea_encrypt(buf, key);
console.timeEnd(`JS run ${run + 1}`);
}
console.groupEnd();
// Benchmark WASM (5 runs)
let wasmTotal = 0;
console.group('WASM Benchmarks (10MB)');
for (let run = 0; run < 5; run++) {
const buf = input.slice();
console.time(`WASM run ${run + 1}`);
tea_encrypt(buf, key);
console.timeEnd(`WASM run ${run + 1}`);
}
console.groupEnd();
console.log('💡 WASM typically 2-5x faster on tight computational loops like this cipher.');
console.log('📊 Results depend on browser/engine (Chrome/V8 shows biggest gains).');
}
runBenchmark().catch(console.error);
</script>
</head>
<body>
<h1>TEA Encryption: WASM vs JS Performance</h1>
<p>Open console to see benchmarks. This uses the Tiny Encryption Algorithm (TEA) in ECB mode on a 10MB buffer.</p>
<p><strong>Setup:</strong> See instructions below to build the WASM module.</p>
</body>
</html>