1

Making a game... More efficient to do this?

if (37 in keysDown) { //left arrow
    if (sprite.state != 'left') sprite.state = 'left';
}

or this?

if (37 in keysDown) { //left arrow
    sprite.state = 'left';
}

This is being called in my game's update function (constantly, as fast as possible).

Sidenote: here is my input key checking code.

//input
var keysDown = {};
window.addEventListener('keydown', function(e) {
    keysDown[e.keyCode] = true;
});
window.addEventListener('keyup', function(e) {
    delete keysDown[e.keyCode];
});
Sam
  • 313
  • 4
  • 17
  • 2
    keydown and keyup will trigger events, use those triggers – Popnoodles Dec 27 '12 at 00:43
  • 1
    @popnoodles: I assume he wants it to trigger as long as the key is held down, in a game's loop; not once, and not on autorepeat. – Amadan Dec 27 '12 at 00:44
  • I know. keydown triggers down is true, keyup trigger down is false. Googling "game loop" will get some results but it's pretty straight forward anyhow. – Popnoodles Dec 27 '12 at 00:44
  • 1
    I doubt that this is generally answerable, since this may (and will) be different for different JS runtimes. – Lucero Dec 27 '12 at 00:49
  • @Lucero: I doubt that an assignment can ever be slower than a retrieval/comparison operation. – Thilo Dec 27 '12 at 00:53
  • That was a lively discussion. – Jared Farrish Dec 27 '12 at 01:03
  • jsperf: http://jsperf.com/one-if-s-or-two If anybody wants to improve/fix/modify, please feel free. – Jared Farrish Dec 27 '12 at 01:07
  • 3
    Thanks all. popnoodles' comment is the most useful to my case, as I am using keydown and keyup events to add/delete keys from an array. simply setting sprite.state on these events will be the best way to do this. – Sam Dec 27 '12 at 01:08
  • 1
    @Thilo, that depends how expensive a write operation is behind the scenes. Reads have less impact on caches etc.). Also, this depends on the ratio of reads and writes, e.g. how often would the condition end up as true? – Lucero Dec 27 '12 at 01:21
  • @Lucero, I just can't believe you guys care or interested about it. they're both the same, even if one of them is faster! – gdoron Dec 27 '12 at 01:24
  • 1
    @gdoron, I'm all with you - I tried to raise the awareness that there just isn't a definitive answer for such questions, and that they therefore don't matter. Pretending that the assignment is always faster is just bullocks because the environment is not even known. It's just irrelevant, which is why I cast the first vote to close as not constructive BTW. – Lucero Dec 27 '12 at 01:56

2 Answers2

4

"We should forget about small efficiencies, say about 97% of the time: Premature optimization is the root of all evil."

http://c2.com/cgi/wiki?PrematureOptimization

Those two ways share the same complexity, setting\changing a four chars variable won't be the bottle-neck in your app.

The only things I'm concerned here is the readability of your code, if either way you want sprite.state to have the value left why do you need to check what was the previous value?
(ohh, and it saves like 20 bits of bandwidth which is just like the performance gain here...)

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 2
    What does the complexity matter? One O(1) can still be a lot slower than another O(1). – Thilo Dec 27 '12 at 00:53
  • 3
    Comeone - the only valid answer here "relax, it doesn't matter" – zerkms Dec 27 '12 at 00:54
  • @Thilo, `O(1) +2` == `O(1) +3` to me(and math...) – gdoron Dec 27 '12 at 00:55
  • So let's all agree to delete all that O(1)/O(N) stuff. – Thilo Dec 27 '12 at 00:56
  • (I took the offer - also made the quote a tad smaller. ## is WAY LOUD; the point of being uncomfortably so.. but hurrah for including 97/3 part of the excerpt.) –  Dec 27 '12 at 01:00
  • @pst, thanks, but sometimes you need to shout so people will start listen and won't care about things like that `;)`. thanks for the edit. – gdoron Dec 27 '12 at 01:02
  • 2
    @gdoron I wasn't trying to take away the shout, just the ear bleed (the clots are counter productive) :) –  Dec 27 '12 at 01:03
  • In case anybody is interested in the full diatribe, see http://c2.com/cgi/wiki?PrematureOptimization. If you have a better link, please let me know. – Jared Farrish Dec 27 '12 at 01:14
  • I think this is more to root of the matter: *Another common misconception is that any level of execution speed, or resource usage, can be achieved once the code is complete. There are both **practical** and **physical limits** given **any target platform**. `PrematureOptimization` is not a solution to this, but it can help us `DesignForPerformance`. **When working in an environment where resources are less limited, this is unlikely to be a problem**.* – Jared Farrish Dec 27 '12 at 01:29
0

Wouldn't something like this be a lot faster than looping through the keys? You can continuously check a variable very quickly.

var keyleft=false;

window.onkeydown = keydown;
window.onkeyup = keyup;

function keydown(event)
{
    var keyCode = ('which' in event) ? event.which : event.keyCode;
    if (keyCode==37) keyleft=true;
}

function keyup(event)
{
    var keyCode = ('which' in event) ? event.which : event.keyCode;
    if (keyCode==37) keyleft=false;
}

No guarantees this code will work, I've forgotten how to do things without jQuery

Popnoodles
  • 28,090
  • 2
  • 45
  • 53
  • 2
    He is not looping through keys. He is storing currently pressed keys in a hash (insert at `keydown`, remove at `keyup`), so he can test for any key at any time if it is down or not. The loop is the game loop (animation speed), not the key loop. – Amadan Dec 27 '12 at 01:06
  • Oh is that what he means by (37 in keysDown) – Popnoodles Dec 27 '12 at 01:17
  • popnoodles, he's not: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/in I knew this, but in my comment had a brainfart and referenced the wrong thing. This use of `in` is correct and not indicative of a loop in the `if`. – Jared Farrish Dec 27 '12 at 01:17
  • It's the same as in_array in PHP but for some reason I was under the impression it was checking keys. – Popnoodles Dec 27 '12 at 01:19