1'use strict'; 2const common = require('../common.js'); 3 4// The following benchmark measures setting up n * 1e6 timeouts, 5// which then get executed on the next uv tick 6 7const bench = common.createBenchmark(main, { 8 n: [1e7], 9}); 10 11function main({ n }) { 12 let count = 0; 13 14 // Function tracking on the hidden class in V8 can cause misleading 15 // results in this benchmark if only a single function is used — 16 // alternate between two functions for a fairer benchmark 17 18 function cb() { 19 count++; 20 if (count === n) 21 bench.end(n); 22 } 23 24 function cb2() { 25 count++; 26 if (count === n) 27 bench.end(n); 28 } 29 30 for (let i = 0; i < n; i++) { 31 setTimeout(i % 2 ? cb : cb2, 1); 32 } 33 34 bench.start(); 35} 36