Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nanotiming overlapping timers
// Three operations (loops for simplicity).
// We want to time the first two separately and all three of them together.
// Ideally, the ordering of starting the timers would not matter.
// This case fails because the timer for the first two loops is started first
var nanotiming = require('nanotiming')
try {
var performance = require('perf_hooks').performance
var timeFirstTwo = nanotiming('my-loop')
var timeAll = nanotiming('my-loop')
} catch (e) {
console.log('perf_hooks not available, exiting')
process.exit(1)
}
var i = 10
while (--i) console.log(i)
var i = 10
while (--i) console.log(i)
timeFirstTwo()
reportTiming()
var i = 10
while (--i) console.log(i)
timeAll()
reportTiming()
function reportTiming() {
var timings = performance.getEntries()
// This assumes that the last one is the one we want
var timing = timings[timings.length - 1]
console.log(timing.name, timing.duration) // log the last entry
performance.clearMeasures(timing.name) // be a good citizen and free after use
}
// This case succeeds because the timer for the first two loops is started last
var nanotiming = require('nanotiming')
try {
var performance = require('perf_hooks').performance
var timeAll = nanotiming('my-loop')
var timeFirstTwo = nanotiming('my-loop')
} catch (e) {
console.log('perf_hooks not available, exiting')
process.exit(1)
}
var i = 10
while (--i) console.log(i)
var i = 10
while (--i) console.log(i)
timeFirstTwo()
reportTiming()
var i = 10
while (--i) console.log(i)
timeAll()
reportTiming()
function reportTiming() {
var timings = performance.getEntries()
// This assumes that the last one is the one we want
var timing = timings[timings.length - 1]
console.log(timing.name, timing.duration) // log the last entry
performance.clearMeasures(timing.name) // be a good citizen and free after use
}
// This case succeeds because the callback to stop timing
// gets the measure name, which means we can reference
// it with getEntriesByName(...)
// This requires a change to nanotiming line 34 to be
// if (cb) cb(err, measureName)
var nanotiming = require('nanotiming')
// Three operations (loops for simplicity).
// We want to time the first two and all three of them.
// Ideally, the ordering of operations would not matter.
try {
var performance = require('perf_hooks').performance
// Start the timers
// This fails:
var timeFirstTwo = nanotiming('my-loop')
var timeAll = nanotiming('my-loop')
// This works:
// var timeAll = nanotiming('my-loop')
// var timeFirstTwo = nanotiming('my-loop')
} catch (e) {
console.log('perf_hooks not available, exiting')
process.exit(1)
}
var i = 10
while (--i) console.log(i)
var i = 10
while (--i) console.log(i)
timeFirstTwo(reportTiming)
var i = 10
while (--i) console.log(i)
timeAll(reportTiming)
function reportTiming(err, name) {
// NOTE: name has to be the measurement name; this is not
// currently the case with nanotiming.
var timing = performance.getEntriesByName(name)[0]
console.log(timing.name, timing.duration) // log the last entry
performance.clearMeasures(timing.name) // be a good citizen and free after use
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment