Last active
March 15, 2018 09:06
-
-
Save agreatfool/774ea9da346fae6891522526cee70aea to your computer and use it in GitHub Desktop.
event-loop-lag.js
This file contains 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
#!/usr/bin/env node | |
"use strict"; | |
const interval = 1000; // 采样间隔,即在这个间隔的自然时间内,调用lag()得到的结果将只会得到同样的返回 | |
const lag = require('event-loop-lag')(interval); | |
function consume() { | |
function fabonacci(n) { | |
if (n === 0) { | |
return 0; | |
} | |
if (n === 1) { | |
return 1; | |
} | |
return fabonacci(n - 1) + fabonacci(n - 2); | |
} | |
const n = 41; | |
const start = new Date(); | |
const result = fabonacci(n); | |
const end = new Date(); | |
console.log('fabonacci(%d) = %d, time used: %d ms.', n, result, end.getTime() - start.getTime()); | |
} | |
// report event loop lag | |
setInterval(() => { | |
console.log('loop lag: %d ms', lag().toFixed(2)); | |
}, 1000); // interval 1s | |
// consumption | |
setInterval(() => { | |
consume(); | |
}, 5000); // interval 5s | |
// $ ./event-loop-lag.js | |
// loop lag: 1.21 ms | |
// loop lag: 2.41 ms | |
// loop lag: 0.07 ms | |
// loop lag: 3.9 ms | |
// fabonacci(41) = 165580141, time used: 2050 ms. | |
// loop lag: 2049.3 ms | |
// loop lag: 2.62 ms | |
// loop lag: 2.85 ms | |
// loop lag: 5.19 ms | |
// loop lag: 3.13 ms | |
// fabonacci(41) = 165580141, time used: 3234 ms. | |
// loop lag: 3225.4 ms | |
// loop lag: 4.58 ms | |
// loop lag: 1.79 ms | |
// loop lag: 0.85 ms | |
// loop lag: 3.89 ms | |
// fabonacci(41) = 165580141, time used: 3214 ms. | |
// loop lag: 3208.08 ms | |
// loop lag: 1.49 ms | |
// loop lag: 3.96 ms | |
// loop lag: 3.2 ms | |
// loop lag: 5.37 ms | |
// fabonacci(41) = 165580141, time used: 3263 ms. | |
// loop lag: 3252.5 ms | |
// loop lag: 1.59 ms | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment