node/benchmark/perf_hooks/timerfied.js
Vinicius Lourenço a6ad048b89
perf_hooks: reduce overhead of new performance_entries
PR-URL: https://github.com/nodejs/node/pull/49803
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2023-09-27 01:19:26 +00:00

37 lines
664 B
JavaScript

'use strict';
const assert = require('assert');
const common = require('../common.js');
const {
PerformanceObserver,
performance,
} = require('perf_hooks');
function randomFn() {
return Math.random();
}
const bench = common.createBenchmark(main, {
n: [1e5],
observe: ['function'],
});
let _result;
function main({ n, observe }) {
const obs = new PerformanceObserver(() => {
bench.end(n);
});
obs.observe({ entryTypes: [observe], buffered: true });
const timerfied = performance.timerify(randomFn);
bench.start();
for (let i = 0; i < n; i++)
_result = timerfied();
// Avoid V8 deadcode (elimination)
assert.ok(_result);
}