profiling-script.js (959B)
1 (function(global) { 2 let counter = 0; 3 4 // Spins up a new profiler and performs some work in a new top-level task, 5 // calling some builtins. Returns a promise for the resulting trace. 6 const profileBuiltinsInNewTask = () => { 7 // Run profiling logic in a new task to eliminate the caller stack. 8 return new Promise(resolve => { 9 setTimeout(async () => { 10 const profiler = new Profiler({ sampleInterval: 10, maxBufferSize: 10000 }); 11 for (const deadline = performance.now() + 500; performance.now() < deadline;) { 12 // Run a range of builtins to ensure they get included in the trace. 13 // Store this computation in a variable to prevent getting optimized out. 14 counter += Math.random(); 15 counter += performance.now(); 16 } 17 const trace = await profiler.stop(); 18 resolve(trace); 19 }); 20 }); 21 } 22 23 global.ProfilingScript = { 24 profileBuiltinsInNewTask, 25 } 26 })(window);