const warmUpCount = 8 const iterationCount = 64 class C {} C.C0=1 C.C1=1 C.C2=2 C.C3=3 C.C4=4 C.C5=5 C.C6=6 C.C7=7 C.C8=8 C.C9=9 C.C10=10 C.C11=11 C.C12=12 C.C13=13 C.C14=14 C.C15=15 no_folding_const_refs_test = benchmarkAverage(no_folding_const_refs_test); no_folding_const_literals_test = benchmarkAverage(no_folding_const_literals_test); folding_test = benchmarkAverage(folding_test); console.log(`no_folding_const_refs_test: ${no_folding_const_refs_test} ns`) console.log(`no_folding_const_literals_test: ${no_folding_const_literals_test} ns`) console.log(`folding_test: ${folding_test} ns`) function benchmarkAverage(func) { console.log(`function ${func.name}:`) console.log("warm up...") for (let i = 0; i < warmUpCount; i++) { func(); } console.log("warm up completed.") console.log("benchmark...") let startTime = process.hrtime(); for (let i = 0; i < iterationCount; i++) { func(); } let elapsed = process.hrtime(startTime); let elapsedNs = Math.round((elapsed[0] * 1e6 + elapsed[1] / 1e3) * 1000 / iterationCount); console.log("benchmark completed.") console.log("") return elapsedNs; } function no_folding_const_refs_test() { return (1 << C.C0) | (1 << C.C1) | (1 << C.C2) | (1 << C.C3) | (1 << C.C4) | (1 << C.C5) | (1 << C.C6) | (1 << C.C7) | (1 << C.C8) | (1 << C.C9) | (1 << C.C10) | (1 << C.C11) | (1 << C.C12) | (1 << C.C13) | (1 << C.C14) | (1 << C.C15); } function no_folding_const_literals_test() { return (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15); } function folding_test() { return 0xFFFF }