2 * This is a simple addition to allow for higher resolution timers.
3 * It can be used to track time for both synchronous or asynchronous
4 * calls. For synchronous calls pass a callback function like so:
6 * var timer = require('./_bench_timer');
8 * timer('myTest', function() {
9 * for (var i = 0; i < 1e6; i++)
10 * // ... run something here
14 * For asynchronous timers just pass the name. Then run it again with
15 * the same name to finish it:
17 * timer('checkAsync');
18 * setTimeout(function() {
19 * timer('checkAsync');
22 * When this happens all currently queued benchmarks will be paused
23 * until the asynchronous benchmark has completed.
25 * If the benchmark has been run with --expose_gc then the garbage
26 * collector will be run between each test.
28 * The setTimeout delay can also be changed by passing a value to
36 var processing = false;
38 var GCd = typeof gc !== 'function' ? false : true;
40 function timer(name, fn) {
41 if (maxLength < name.length)
42 maxLength = name.length;
47 store[name] = process.hrtime();
50 displayTime(name, process.hrtime(store[name]));
56 if (!processing && asyncQueue <= 0) {
58 setTimeout(run, timer.delay);
65 if (asyncQueue > 0 || order.length <= 0)
68 setTimeout(function() {
69 var name = order.shift();
71 var ini = process.hrtime();
73 ini = process.hrtime(ini);
74 displayTime(name, ini);
79 function displayTime(name, ini) {
81 while (name.length < maxLength + 2)
83 console.log(name + '%s \u00b5s',
84 (~~((ini[0] * 1e6) + (ini[1] / 1e3)))
85 .toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
88 module.exports = timer;