How can we delete array in javascript?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

How can we delete array in javascript?

 
0
  #1
Aug 19th, 2009
Hi all,

I want help from all you wonderful guys. I want to know how can we free memory occupied by an Array. That means all the elements associated with this array should be deleted.

let say:

  1. var bigArray = ["313123", "123123", "sadasd",.........................];

So which will be the best and assured way :

1)
  1. delete bigArray;
OR
2)
  1. bigArray = [];
OR
3) any other ...

Thanks,
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: How can we delete array in javascript?

 
0
  #2
Aug 19th, 2009
wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: How can we delete array in javascript?

 
0
  #3
Aug 19th, 2009
Hi everyone,

Lucky,
here's a simple demo, showing how you can removed items inside your array using the pop(index) method.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">
  7. <title>www.daniweb.com</title>
  8. <script type="text/javascript">
  9. <!--
  10.  
  11. window.onload = function() {
  12. var bigArray = [];
  13.  
  14. for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>>
  15. bigArray[ i ] = "Item" + i;
  16. } alert("bigArray has a total of " + ( bigArray.length ) + " items inside its collection");
  17. var aLen = ( bigArray.length - 1 );
  18. for ( var x = aLen; bigArray[ x ]; x-- ) { // Removing all 500 items from the bigArray.
  19. bigArray.pop( x );
  20. } alert( "\nAll items' from the ( bigArray ) has been removed.\nItem count: " + bigArray.length );
  21. };
  22. // -->
  23. </script>
  24. </head>
  25. <body>
  26. <div></div>
  27. </body>
  28. </html>
Last edited by essential; Aug 19th, 2009 at 3:26 am.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: How can we delete array in javascript?

 
0
  #4
Aug 19th, 2009
i dont think he is looking for such a processor consuming way of deleting the memory. i think delete keyword works better for his needs. or basically setting the array to null.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: How can we delete array in javascript?

 
0
  #5
Aug 19th, 2009
Obtaining serkan's adviced of way of freeing the memory and minimized the above application:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
   var bigArray = [];  
   bigArray2 = [];
   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
      bigArray2[ i ] = "Item" + i;
   } 
/*
  You can use the delete operator to delete variables declared implicitly but not those declared with the var statement. 

Output Sample: */

delete bigArray; // ignore's the delete process.

alert( bigArray.length ); // outputs the same length

// either one of the following statement will work, if you have defined it using the var statement.

bigArray = null; // or:

// bigArray = []; // reset's all items...

delete bigArray2; // this one works and delete's the bigArray2 variable.

};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: How can we delete array in javascript?

 
0
  #6
Aug 19th, 2009
Thanks for taking me into consideration
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: How can we delete array in javascript?

 
0
  #7
Aug 19th, 2009
Thanks gentlemen,

So, using 'delete' OR '[]' will delete all the items (and memory) associated with the array.

Please confirm this.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: How can we delete array in javascript?

 
0
  #8
Aug 19th, 2009
Hi lucky,

both ways will provide the same results, the only difference between the two procedure, is that when you use the delete operator, your variable will no longer be accessible within the function unless you redefined it.

Hope we've claimed your needs...

@serkan - welcome , you had a better solution compared to my first post.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: How can we delete array in javascript?

 
0
  #9
Aug 19th, 2009
Originally Posted by serkan sendur View Post
wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?
We usually encounter this problem when working with embedded devices (settop boxes etc.) where there is very less memory. We need to free memory where ever its possible.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: How can we delete array in javascript?

 
0
  #10
Aug 19th, 2009
Appreciated!
Thanks to all.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC