- Home /
How do I Resize an Array in JS?
So, Newbie me just got introduced to .NET array methods by our shepherd Eric5h5. Tried System.Array.Copy, nearly made me cry with a mixture of joy at the prospect of not having to write a new function for each new class I'm resizing an array of, and frustration with having already typed so much useless code... I then tried System.Array.Resize(myArray, myArray.length+1); nope. Is my syntax wrong, or is there only a limited set of .NET methods which we can use in Unity's javascript? If so, where's the list? And are there any iOS compatibility issues? If yes, it would at least make my array manipulation skills less obsolete...
Thanks in advance!
Why not use lists? They have functionality that arrays do not. Arrays aren't really meant to be resized or anything- they're the simplest data structure.
I fully agree with @syclamoth that Lists are superior to arrays for dynamic size scenarios. I understand that you might want to fully grasp the methods coupled with arrays, but the brutal practical fact is that you'll be using these functions extremely rarely. I've worked professionally for 3-4 years and I've been program$$anonymous$$g C# for several years more and I've never had to use it, not even once. You'll be doing yourself a great favor if you move on to List, which is a typical class you'll be working with a lot.
Answer by Statement · Dec 13, 2011 at 11:37 PM
System.Array.Resize is a template method and the only way I can get it to work with JS is to explicitly define the template parameter. So yes, your syntax was a bit off there, but here's how it should look:
var myArray : int[];
System.Array.Resize.<int>(myArray, myArray.length + 1);
print(myArray.length);
Given myArray is initially 5, running this script will print "6". I hope this little snippet is self explainable but if you have questions, just post a comment to this answer and I or someone else will get back to you.
The code you posted would generate this error message:
Assets/MyArray.js(2,20): BCE0023:
No appropriate version of 'System.Array.Resize' for the argument list '(int[], int)' was found.
The method clearly is there, it's just hidden as a template method (meaning you can replace the type of the array). The syntax for specifying the template parameter is added as .<T>
to the method name, where T is the type of elements in the array.
Thanks , I was expecting it had something to do with type, seeing that T, but didn't know the proper syntax... Are lists as fast as arrays? I need the code to be super efficient, as I'm resizing big 2d arrays quite often in a musical game based on user friendly, custom built sequencers, and sync needs to be perfect. What about iOS compatibility? No worries there? Will take a look at List anyway.
Arrays are faster in raw access speed, but lists will manage memory faster and more efficiently than resizing arrays frequently, as in your example. I answered a related topic a while ago. Arrays are faster, but read/write is such a small overhead these days. Whatever you are doing per object in an array is probably going to take significantly longer time to process making the difference negligible. It is not the raw access speed you should be concerned about, but what you are doing per object. Your memory usage patterns could benefit from special memory curves for instance if you frequently access objects left, right up and down an element in a 2d array. By organizing your objects in different ways you can benefit from your computers cache, avoiding cache misses and RA$$anonymous$$ lookups which can boost performance a lot. But the best optimization you can make is figuring out how you can make the least amount of work that you need to. For instance, if you wanted to check which object out of 10000 objects are closest to you, you'd naively check each object and keep track of the one that has the least distance to you. A better approach for this would be to divide and conquer the problem by grouping objects in a hierarchical manner and effectively cull (ignore) the objects that aren't anywhere near. Ins$$anonymous$$d of testing 10000 objects, you might end up testing only 10 objects. That is how optimization should be done, not deciding too much about lower detail stuff, but of course it adds to it. The question is how productive you'll be with everything super optimized and how much bang you get for the buck.
$$anonymous$$y bottom line is that you should try to use what's easy and readily available and start focusing on optimization once you notice that there is a pressing need for it (by using a profiler for example). The exception to this is if you're an experienced programmer and know for sure that a certain optimization is necessary to perform a certain task, then you probably are better off implementing the optimization ahead of time. But some people will disagree with me.
Note that the speed difference depends on the type used in the array. An int[]
array, for example, was about 6X faster than List.<int>
last I checked. However, a GameObject[]
array is barely any different than List.<GameObject>
. The simpler the type, the greater the speed difference.
Note also that Lists really just do System.Array.Resize on fixed-length arrays anyway when necessary--they start out with a capacity of 4 (unless otherwise specified), and keep doubling that any time the capacity is exceeded. They don't reduce capacity automatically, which is why the TrimExcess method exists (in case you had a really large list, then removed a lot of elements, and want to reclaim the memory). There's not actually such a thing as a true variable-length array in .NET, it's just that List creates the illusion of a variable-length array. So Lists aren't necessarily going to be better for memory than resizing arrays, but they save you the bother of having to do all that array management yourself, which usually results in better code.
Thanks to all! I think one of the beginner's mistakes I am making is using array.length to keep track of how many objects I've got, hence all the resizing. From what I understand from your discussion, I could have a much bigger 2d array that I don't resize,extra variables to keep track of which parts of the array I'm iterating through, and that would be much more efficient than resizing all the time(which, you can laugh, I was doing thinking it would save resources). Then use Lists for complex classes such as gameObjects. Correct me if I'm wrong! I'm working on a music game/real time instrument, hence all the attention to optimization: framerate drop is my nemesis... So I'm trying to learn right! Thanks again