Skip to content

Instantly share code, notes, and snippets.

@felixge

felixge/blocked-by-loop.js

Last active Feb 19, 2020
Embed
What would you like to do?
let last = new Date();
setInterval(function () {
delta = new Date() - last;
if (delta > 2) {
console.log(delta);
}
last = new Date();
}, 1);
setInterval(function () {
console.log('loop start');
for (let i = 0; i < Math.pow(10, 8); i++) {};
console.log('loop end');
}, 1000);
$ node blocked-by-loop.js
loop start
loop end
76
loop start
loop end
69
4
4
loop start
loop end
71
loop start
loop end
71
loop start
loop end
61
loop start
loop end
59
loop start
loop end
60
loop start
loop end
60
loop start
loop end
61
loop start
loop end
60
loop start
loop end
62
loop start
loop end
70
loop start
loop end
69
loop start
loop end
69
var net = require('net');
var server = net.createServer(function (socket) {
socket
.on('error', console.error)
.on('data', function () {
socket.pause();
setImmediate(function () {
for (let i = 0; i < Math.pow(10, 8); i++) {};
socket.resume();
});
});
});
server.listen(1337, '127.0.0.1');
let last = new Date();
setInterval(function () {
delta = new Date() - last;
if (delta > 2) {
console.log(delta);
}
last = new Date();
}, 1);
$ node blocked-by-net-improved.js &
[1] 6644
3
$ yes | nc 127.0.0.1 1337 > /dev/null
95
81
87
84
82
82
89
82
84
82
83
83
84
85
var net = require('net');
var server = net.createServer(function (socket) {
socket
.on('error', console.error)
.on('data', function () {
for (let i = 0; i < Math.pow(10, 8); i++) {};
});
});
server.listen(1337, '127.0.0.1');
let last = new Date();
setInterval(function () {
delta = new Date() - last;
if (delta > 2) {
console.log(delta);
}
last = new Date();
}, 1);
$ node blocked-by-net.js
3
5
3
1785
1771
1767
1783
1778
1785
1777
1778
1795
233
3
3
let last = new Date();
setInterval(function () {
delta = new Date() - last;
if (delta > 2) {
console.log(delta);
}
last = new Date();
}, 1);
$ node blocked-by-nothing.js
3
3
5
4
3
4
4
5
4
3
3
3
4
3
3
5
3
3
3
6
$ node --version
v12.12.0
$ uname -a
Darwin felix-mbp-2017.local 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment