Before updating to Windows 10 you may need to return your Pro license. More information here

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!

more ▼

asked Dec 13, 2011 at 11:20 PM

avatar image

3.4k 120 90 125

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.

Dec 13, 2011 at 11:24 PM syclamoth

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 programming 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.

Dec 13, 2011 at 11:32 PM Statement
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);

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.

more ▼

answered Dec 13, 2011 at 11:37 PM

avatar image

28.1k 81 183 307

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.

Dec 13, 2011 at 11:51 PM gregzo

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 RAM 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. Instead 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.

My 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.

Dec 14, 2011 at 12:12 AM Statement

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.

Dec 14, 2011 at 12:53 AM Eric5h5

Yes, that's what I said. ;)

Dec 14, 2011 at 01:06 AM Eric5h5

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

Dec 14, 2011 at 01:34 AM gregzo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 13, 2011 at 11:20 PM

Seen: 5514 times

Last Updated: Dec 14, 2011 at 07:16 AM