Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Best way to reset a global array?

I have a global multi-dimensional array. What is the best way to reset the array (clear out all the values from it)? The size will obviously change each time.
var aa ={};
aa.myArray = [];
//...array gets populated somewhere
aa.clearArray = function(){

// which is better
aa.myArray[];
// or
aa.myArray.length = 0;
// or
aa.myArray = null;

}

Open in new window

0
894359
Asked:
894359
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
what about :
aa.myArray = [];

Open in new window

0
 
894359Author Commented:
leakim- I was reading that that method only creates a copy and can have pointer issues.

http://davidwalsh.name/empty-array
0
 
Sar1973Commented:
var aa = new Array();
0
 
leakim971PluritechnicianCommented:
leakim- I was reading that that method only creates a copy and can have pointer issues.

I don't create a copy, it create an empty array ([] same as new Array()) and affect it to your variable. The previous object is not "deleted" so stay in memory. It consume more memory but it's faster. Once the Garbage collector remove object with no reference the object is deleted from memory.

Read the comments.

In your snipet I don't see any << B = A >>
If it's global that mean any function can use it so Iif you've somewhere << B = A >> it's to have an instant copy (and not maintain sync between A and B)
The garbage collector will destruct the previous object without no reference... if needed...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now