Created
March 18, 2018 15:27
-
-
Save fpapado/9163e02e6cf303d5b7bfe113ca5e964f to your computer and use it in GitHub Desktop.
nanotiming overlapping timers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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