Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my two cents:</p> <p>I'd go with keeping a clean copy of your original array for fastest performance. You can either keep a referenced hard-coded copy</p> <pre><code>var vectorsOrig = [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]; </code></pre> <p>or do a dynamic clean clone of the initial array using slice ((recursively in your case for deep copy):</p> <pre><code>var clonedVectors = [0, 0, 0, 0, 0].slice(0); </code></pre> <p>Regardless, taking the approach of <strong>resetting your vector reference to an original copy will be faster than cycling through and resetting each node</strong>. If your old vector array object isn't referenced any more, JavaScript will garbage collect it. </p> <p>With that said, the question becomes of obtaining a clean copy each and every time. Having once hard-coded instance will give you one clean copy and you'll have to clone it thereafter. Nor do you want to into dynamic generation via similar for loops as the reset option. My advice is to write a clone function that simply returns a new hard-coded or initialized array:</p> <pre><code>function newVector() { return [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]; } var vector = newVector(); vector[1][2] = 11; console.dir(vector); vector = newVector(); // your old array will be garbage-collected if no longer referenced by any other reference console.dir(vector); </code></pre> <p>Ideally, it's best to benchmark various approach. </p> <p><strong>EDIT</strong> Thanks to Vega's input, I've modified his test to test three approaches. In Chrome and IE9, this solution seems to be the fastest, in FF (15.0.1) manual iteration seems faster (memory allocation/management slower in FF possibly). <a href="http://jsperf.com/array-zero-test/2" rel="nofollow">http://jsperf.com/array-zero-test/2</a></p>
    singulars
    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.
    3. 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