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

  • Home
  • SEARCH
  • 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 9257821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:14:50+00:00 2026-06-18T12:14:50+00:00

Let’s say that I’d like to store all the bullets, that anyone is shooting

  • 0

Let’s say that I’d like to store all the bullets, that anyone is shooting in my game to calculate new position every frame etc.

If there are 10 players and everyone’s shot rate is 10 shots per second we would possibly need to track 1000 objects after only 10 seconds.

We do know, that iteration over array is very efficient.

should I add new bullets like this?

// "bullets" is an array
bullets.push({
    x_position: 5, // x position from which bullet was shot
    y_position: 10, // same as above but for y
    x_speed: 2, // count of pixels that bullet is travelling on x axis per frame
    y_speed: 10 // as above but for y
});

should I remove bullets, that hit the bound, or another player like this?

delete bullets[i] // i -> currently processed bullet index

Cause if i try to take out the element from bullets array, it’s not very efficient with long arrays.

Honestly i haven’t had any better idea to solve bullets problem. Iteration through this kind of array can be painful after couple of minutes because if we delete old bullets, the array length just stays the same and we end up with iterating over milions of records, from which 99% are just empty.

  • 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-18T12:14:51+00:00Added an answer on June 18, 2026 at 12:14 pm

    I believe that you want to implement a linked list instead of using a JavaScript array.

    The truth about JS arrays

    First, you may have a misconception about arrays. When we think of JavaScript arrays, we’re really talking about hashmaps where the keys just happen to be integers. That’s why arrays can have non-numeric indices:

    L = [];
    L[1] = 4
    L["spam"] = 2;
    

    Iteration is fast for arrays (in the C/C++ sense, at least), but iteration through a hashmap is rather poor.

    In your case, some browsers might implement your array a real array if certain constraints are met. But I’m fairly certain you don’t want a real array either.

    Array performance

    Even a real array isn’t particularly amenable to what you want to do (as you pointed out, your array just keeps filling up with undefined elements, even as you delete bullets!)

    And imagine if you did want to delete bullets from a real array and remove the undefined elements: the most efficient algorithm I can think of involves creating a new array after a full sweep of bullets, copying all the bullets which haven’t been deleted into this new array. This is fine, but we can do better.

    Linked Lists in JavaScript!

    Based on your problem, I think you want the following:

    • fast creation
    • fast iteration
    • fast deletion

    A simple data structure which provides constant time creation, iteration, and deletion is a linked list. (That said, linked lists don’t allow you to get random bullets quickly. If that is important to you, use a tree instead!)

    So how do you implement a linked list? My favorite way is to give each object a next reference, so that each bullet points or “links” to the next bullet in the list.

    Initializing

    Here’s how you might start the linked list off:

    first_bullet = {
        x_position: 5,
        y_position: 10,
        x_speed: 2,
        y_speed: 10,
        next_bullet: undefined, // There are no other bullets in the list yet!
    };
    
    // If there's only one bullet, the last bullet is also the first bullet.
    last_bullet = first_bullet;
    

    Appending

    To add a bullet to the end of the list, you’ll want to set the next reference of the old last_bullet, then move last_bullet:

    new_bullet = {
        x_position: 42,
        y_position: 84,
        x_speed: 1,
        y_speed: 3,
        next_bullet: undefined, // We're going to be last in the list
    };
    
    // Now the last bullet needs to point to the new bullet
    last_bullet.next_bullet = new_bullet;
    
    // And our new bullet becomes the end of the list
    last_bullet = new_bullet;
    

    Iterating

    To iterate through the linked list:

    for (b = first_bullet; b; b = b.next_bullet) {
    
        // Do whatever with the bullet b
    
        // We want to keep track of the last bullet we saw...
        // you'll see why when you have to delete a bullet
        old = b; 
    }
    

    Deleting

    Now for deletion. Here, b represents the bullet being deleted, and old represents the bullet just before it in the linked list — so old.next_bullet is equivalent to b.

    function delete_bullet(old, b) {
        // Maybe we're deleting the first bullet
        if (b === first_bullet) {
            first_bullet = b.next_bullet;
        }
    
        // Maybe we're deleting the last one
        if (b === last_bullet) {
            last_bullet = old;
        }
    
        // Now bypass b in the linked list
        old.next_bullet = b.next_bullet;
    };
    

    Notice that I didn’t delete the bullet using delete b. That’s because delete doesn’t do what you think it does.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I'm creating an OpenGL game in C++ that will have many objects
Let's say I've got a collection of 10 million documents that look something like
Let's say I'm outputting a post title and in our database, it's Hello Y’all
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have pseudocode like this: main() { BOOL b = get_bool_from_environment(); //get
Let's say for a moment that I have the following module in python: class

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.