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.

at master 90 lines 2.3 kB view raw
1const now = () => (typeof performance !== 'undefined' && performance.now ? performance.now() : Date.now()); 2 3function readScale() { 4 if (typeof process === 'undefined' || !process || !process.argv) return 1; 5 const raw = Number(process.argv[2]); 6 return Number.isFinite(raw) && raw > 0 ? raw : 1; 7} 8 9const SCALE = readScale(); 10const REPEATS = 5; 11 12function bench(name, rounds, fn) { 13 fn(Math.max(1, (rounds / 8) | 0)); 14 15 const samples = []; 16 let out = 0; 17 for (let i = 0; i < REPEATS; i++) { 18 const t0 = now(); 19 out = fn(rounds); 20 samples.push(now() - t0); 21 } 22 23 let best = samples[0]; 24 let sum = 0; 25 for (let i = 0; i < samples.length; i++) { 26 if (samples[i] < best) best = samples[i]; 27 sum += samples[i]; 28 } 29 30 const avg = sum / samples.length; 31 console.log(`${name}: best ${best.toFixed(2)} ms, avg ${avg.toFixed(2)} ms, out ${out}`); 32 return out; 33} 34 35function hotMissingParamWrite(rounds) { 36 function f(val, options) { 37 options = options || {}; 38 if (typeof val === 'number') return options.long ? 2 : 3; 39 return 0; 40 } 41 42 let sum = 0; 43 for (let i = 0; i < rounds; i++) sum += f(i); 44 return sum; 45} 46 47function hotPresentParamWrite(rounds) { 48 function f(val, options) { 49 options = options || {}; 50 if (typeof val === 'number') return options.long ? 2 : 3; 51 return 0; 52 } 53 54 const shared = {}; 55 let sum = 0; 56 for (let i = 0; i < rounds; i++) sum += f(i, shared); 57 return sum; 58} 59 60function hotNoParamWrite(rounds) { 61 function f(val, options) { 62 if (!options) options = {}; 63 if (typeof val === 'number') return options.long ? 2 : 3; 64 return 0; 65 } 66 67 const shared = {}; 68 let sum = 0; 69 for (let i = 0; i < rounds; i++) sum += f(i, shared); 70 return sum; 71} 72 73function hotLocalWrite(rounds) { 74 function f(val, options) { 75 let opts = options || {}; 76 if (typeof val === 'number') return opts.long ? 2 : 3; 77 return 0; 78 } 79 80 let sum = 0; 81 for (let i = 0; i < rounds; i++) sum += f(i); 82 return sum; 83} 84 85const rounds = 8_000_000 * SCALE; 86console.log(`jit missing-parameter benchmark (${rounds} rounds)`); 87bench('missing param write', rounds, hotMissingParamWrite); 88bench('present param write', rounds, hotPresentParamWrite); 89bench('no param write', rounds, hotNoParamWrite); 90bench('local alias write', rounds, hotLocalWrite);