Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:57:51+00:00 2026-06-16T09:57:51+00:00

Making a game… More efficient to do this? if (37 in keysDown) { //left

  • 0

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];
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:57:52+00:00Added an answer on June 16, 2026 at 9:57 am

    “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…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a game where you control your character with the arrow keys and
I'm making a Game class with a function that makes a window. When I
I'm currently making a game called Snowflake to learn how to program QML with
I´m making this game but I´ve run into a problem with the structures. I´ve
Im making a game, when my mainCharacter in this game dies, or when he
I'm making a game for class and for this game I have an array
Im making game plugin in java and Ive got problem with split function. index
I'm making a game where you have a sprite that shoots bullets in the
I am making game and need to prepare view for level selection. Could you
I built a slideshow/decision-making game in Flash but would like to try to redo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.