Application is iteratively checking for event flags, often needlessly.
Application is iteratively checking for event flags, often needlessly.CPU usage is high and potentially running inefficiently due to frequent context switches, increasing likelihood of thrashing.
CPU usage is high and potentially running inefficiently due to frequent context switches, increasing likelihood of thrashing.You likely end up writing and event-system and event-driven code anyway ...
while (true) { var flags; if (flags = checkMouseFlags()) { doMouseInput(flags); } if (flags = checkKeyboardFlags()) { doKeyBoardInput(flags); } }
function doMouseInput() { // stop lying to yourself. this is an event. }
function doKeyboardInput() { // this is also an event. }
You likely end up writing and event-system and event-driven code anyway ...
E.g. ...
while (true) {
var flags;
if (flags = checkMouseFlags()) {
doMouseInput(flags);
}
if (flags = checkKeyboardFlags()) {
doKeyBoardInput(flags);
}
}
function doMouseInput() {
// stop lying to yourself. this is an event.
}
function doKeyboardInput() {
// this is also an event.
}
... Alternatively:
Let the host invoke the code when something happens.
Notable: In other languages, and with the advent of worker processes in JavaScript, it's not an either-or paradigm. You can have a background process butchering the CPU and use the existing event-system to do as much or as little with each event as you like.
So, why is JavaScript event-driven?
Because it's reflective of the modeled reality, easy to think about, gentle on the CPU, and it simply extends the host environment to the script.