2

I'm iterating through an array of arrays, pulling out the sub-arrays I need and discarding the rest.

var newArray = [];
for (var i = 0; i < oldArray.length; i++) {
    if (oldArray[i][property] == value) {
        newArray.push(oldArray[i]);
    }
}
oldArray = newArray;
  1. Is this the most memory-friendly way to do this?

  2. Will garbage collection safely take care of the sub-arrays I did not push onto newArray?

  3. Will newArray be scattered across memory in a way that could prevent this method from scaling efficiently?
4

2 に答える 2

2

The javascript prototypal nature makes everything be an object. And all objects are maps, literally hash maps. Which means that when you are "pulling", as you say, objects from one array into another, you are only copying their references.

So yes I would say it won't bring you much memory problems, if you drop the references (at least in modern browsers). But the garbage collectors are implemented in different ways depending on the browser you are working with.

1 - Is this the most memory-friendly way to do this?

If you drop the references, yes it is a memory friendly way to do it. You don't have any free like in c/c++, and for testing purposes on chrome i think you can call window.gc() to call the garbage collector. I think a delete exists or existed but I don't know how it works.

2- Will garbage collection safely take care of the sub-arrays I did not append to newArray?

If there aren't any other references pointing to them. Yes. Circular memory leaks were common in older browsers but with the new ones it's safe to say yes.

3 - Will newArray be scattered across memory in a way that could prevent this method from scaling efficiently?

Yes it will be scattered across memory because in javascript arrays work like hashmaps or linked hashmaps (if i'm mistaken here someone pls correct me) but the garbage collector will take care of it, because it is used to work with maps. And again you are only working with references the objects will keep in the same place and you will only store references in the array.

于 2012-07-06T20:01:55.373 に答える
1

1.no... fmsf is mostly right, but there are some micro-optimization that you could do for page load time improvement, and for the time to check the condition. If you leave oldArray.length in the loop, it will look up length on the oldArray object for every iteration, which can add some time in large loops, also instantiating all your variables at once can also save some time if the method this is contained within is called many times.
When someone downloads your script, it is good to have all the variables with shortest names as possible to have the least data transference from server to client.

var nA = [],
    i = 0,
    t = oA.length,
    s;
for (; i < t; i++) {
    s = oA[i];
    if (s[p] == v) {
        nA.push(s[i]);
    }
}
oA = nA;

If you want to get really serious, you would use a code minifier for the renaming of variables and white space removal.

2.yes, JavaScript is pretty good about these things, the only things you have to look out for is IE closures which would not be caused by your code here. I have still found closures in rare cases to be prevalent in IE9 in odd cases, but these are usually caused by linking to the DOM, which is irrelevant in this case.

3.no, the only things that are changing with this code are refrences to objects, not how they are stored in memory.

于 2012-07-06T20:10:04.573 に答える