www.webdeveloper.com
Results 1 to 2 of 2

Thread: Which what to best re-initialize or clear an array?

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question Which what to best re-initialize or clear an array?

    In the following demo only script, I clear (reset) the "tarr" two different ways.
    My questions are:
    1. Is one way better than the other? More acceptable? Or are they both the same?
    2. Is there a concern with "memory leakage" using either method?

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Clear Array Contents </title>
    
    </head>
    <body>
    <button onclick="initArr(arr1)">Initial Array (1..5)</button>
    <button onclick="initArr(arr2)">Initial Array (5..1)</button>
    <button onclick="initArr(arr1.concat(arr2))">concat Arrays</button>
    
    <p>
    <!-- <button onclick="showArr(tarr)">Show tarray</button> -->
    
    <button onclick="clearArr1(tarr)">Clear []</button>
    <button onclick="clearArr2(tarr)">Clear =0</button>
    <p>
    <div id="debug"></div>
    
    <script type="text/javascript">
    var arr1 = [1,2,3,4,5];
    var arr2 = [5,4,3,2,1];
    
    var tarr = [];  // global array
    
    function initArr(arr) { tarr = arr.slice(0);  showArr(tarr); }
    function showArr(arr) { document.getElementById('debug').innerHTML = arr; }
    
    function clearArr1(arr) { tarr = [];  showArr(tarr); }
    function clearArr2(arr) { tarr.length = 0;  showArr(tarr); }
    
    </script>
    
    </body>
    </html>

  2. #2
    Join Date
    Mar 2009
    Posts
    495
    Greetings----

    You pose an interesting question. I had no idea as to the answer, and I might not still. But I did conduct a speed test about 20 or so times, and it appears that assigning the variable to an empty array is significantly faster than setting the length property to zero. I ran the program using node,js which uses Chrome's javascript engine It may be however that you will get wildly different results with different js engines.

    Code:
    function lenR(){
        var arr, st, fin, i, d = new Date();
        st = d.getTime();
        for(i = 0; i < 10000000; i++){
    	arr = [1,2,3,4,5,6,7,8,9,0];
    	arr.length = 0;
        }
        d = new Date();
        fin = d.getTime();
        console.log("time for setting length method was " + (fin - st)+ " millisecs");
    }
    
    function lenE(){
        var arr, st, fin, i, d = new Date();
        st = d.getTime();
        for(i = 0; i < 10000000; i++){
    	arr = [1,2,3,4,5,6,7,8,9,0];
    	arr = [];
        }
        d = new Date();
        fin = d.getTime();
        console.log("time for assigning to an empty array was " + (fin - st)+ " millisecs");
    }
    
    lenR();
    lenE();

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles