Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe that you want to implement a linked list instead of using a JavaScript array.</p> <h1>The truth about JS arrays</h1> <p>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:</p> <pre><code>L = []; L[1] = 4 L["spam"] = 2; </code></pre> <p>Iteration is fast for arrays (in the C/C++ sense, at least), but iteration through a hashmap is rather poor.</p> <p>In your case, some browsers might implement your array a real array if <a href="http://www.html5rocks.com/en/tutorials/speed/v8/" rel="nofollow">certain constraints</a> are met. But I'm fairly certain you don't want a real array either.</p> <h1>Array performance</h1> <p>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 <code>undefined</code> elements, even as you delete bullets!)</p> <p>And imagine if you did want to delete bullets from a real array and remove the <code>undefined</code> 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.</p> <h1>Linked Lists in JavaScript!</h1> <p>Based on your problem, I think you want the following:</p> <ul> <li>fast creation</li> <li>fast iteration</li> <li>fast deletion</li> </ul> <p>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!)</p> <p>So how do you implement a linked list? My favorite way is to give each object a <code>next</code> reference, so that each bullet points or "links" to the next bullet in the list.</p> <h2>Initializing</h2> <p>Here's how you might start the linked list off:</p> <pre><code>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; </code></pre> <h2>Appending</h2> <p>To add a bullet to the end of the list, you'll want to set the <code>next</code> reference of the old <code>last_bullet</code>, then move <code>last_bullet</code>:</p> <pre><code>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; </code></pre> <h2>Iterating</h2> <p>To iterate through the linked list:</p> <pre><code>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; } </code></pre> <h2>Deleting</h2> <p>Now for deletion. Here, <code>b</code> represents the bullet being deleted, and <code>old</code> represents the bullet just before it in the linked list --- so <code>old.next_bullet</code> is equivalent to <code>b</code>.</p> <pre><code>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; }; </code></pre> <p>Notice that I didn't delete the bullet using <code>delete b</code>. That's because <a href="http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/" rel="nofollow"><code>delete</code> doesn't do what you think it does.</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload