MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add in-depth microbench2

+57
+57
examples/demo/microbench2.js
··· 1 + function add(a, b) { 2 + return a + b; 3 + } 4 + 5 + function now() { 6 + if (typeof performance === 'object' && performance && typeof performance.now === 'function') { 7 + return performance.now(); 8 + } 9 + return Date.now(); 10 + } 11 + 12 + function benchEmpty(iterations) { 13 + let result = 0; 14 + for (let i = 0; i < iterations; i++) { 15 + result = i; 16 + } 17 + return result; 18 + } 19 + 20 + function benchInline(iterations) { 21 + let result = 0; 22 + for (let i = 0; i < iterations; i++) { 23 + result += i + (i + 1); 24 + } 25 + return result; 26 + } 27 + 28 + function benchCall(iterations) { 29 + let result = 0; 30 + for (let i = 0; i < iterations; i++) { 31 + result += add(i, i + 1); 32 + } 33 + return result; 34 + } 35 + 36 + function time(name, iterations, fn) { 37 + let start = now(); 38 + let result = fn(iterations); 39 + let elapsed = now() - start; 40 + console.log(`${name}: ${elapsed.toFixed(3)}ms (result: ${result})`); 41 + } 42 + 43 + const iterations = Number(process.argv[2] || 2000000); 44 + const warmupIterations = Math.min(iterations, 200000); 45 + 46 + console.log(`Running ${iterations} iterations...`); 47 + 48 + time('cold call add', iterations, benchCall); 49 + 50 + benchEmpty(warmupIterations); 51 + benchInline(warmupIterations); 52 + benchCall(warmupIterations); 53 + 54 + time('warm empty loop', iterations, benchEmpty); 55 + time('warm inline add', iterations, benchInline); 56 + time('warm call add', iterations, benchCall); 57 + console.log('Done!');