Jump to content
shadowayex

Best practice for ever changing arrays.

Recommended Posts

I've made a sort of structure that works something like this:I have an array (arr) that is instantiated.I have two functions, one that adds things to arr based on user input, and one that removes things from arr, again based on user input.The first one returns the index, which is then stored to be used in the remove function, if the user so chooses to remove said item.As of right now, the adding function just tacks the new item on at the index arr.length, and the remove function sets whatever index is being passed to null. This can be done many, many times before the page is reloaded, so arr.length could get very large, although most of the indices could be null.I was considering modifying the add function to cycle through and look for the first null index and put the new item there, that way the arr.length ends up slightly reflecting the amount of items added (or at least the max at one time), rather than the number added ever.I was wondering what performance issues keeping my current design could have, as compared to my new design idea. Which one would be better? In there a better design all around that I have not thought of?I hope this is all clear. I'm open to any and all input.

Share this post


Link to post
Share on other sites

Consider using array.push and array.spliceI'd use push to add the item to the array and return an index like you are currently doing. Then using that index in the remove function, I'd use splice to remove said item.

Share this post


Link to post
Share on other sites
push() only puts things at the end, which to my knowledge is without any consideration for the values of the indicies in between. As an alternative to using array.length in the add function, then yes, that seems like an alternative, although not central to the OP's questions at hand.Maybe it depends on what you end up doing with the array at the end of all this? For your remove function, have you considered just using splice()?http://www.w3schools.com/jsref/jsref_splice.aspIt's kind of a similar situation that some people encounter when managing databases when a record is removed but auto-increment doesn't fill in the gaps as you keeping adding records. Edited by thescientist

Share this post


Link to post
Share on other sites

Well, when it's all said and done, the array is sent to the server in a JSON format and all of the items sent are removed, then the user starts over.Does splice change indices? Because if so, then this wouldn't work, as I need the indices for items in the array to remain constant, so my remove function can remove them at any time.I just tried this, and I don't seem to be getting good results. I'll have to play with it more to ensure I've got my code set up properly, though.

Share this post


Link to post
Share on other sites
Well, when it's all said and done, the array is sent to the server in a JSON format and all of the items sent are removed, then the user starts over.Does splice change indices? Because if so, then this wouldn't work, as I need the indices for items in the array to remain constant, so my remove function can remove them at any time.I just tried this, and I don't seem to be getting good results. I'll have to play with it more to ensure I've got my code set up properly, though.
well, I guess the simplest option is to just add using push() and keep your remove functionality the same. After all is said and done, you can create another function to filter the array for only non-null indicies before submitting it.

Share this post


Link to post
Share on other sites

And, lets say the user had some sort of fit where he or she added like 500 things, removed the first 490 of them, then added 500 more. The array length would be 1000, but the first 490 would be empty. My system already ignores this, but is that bad on resources or anything? Should I place logic to, instead of push()-ing, check for the first null item and add at the index instead? Or would the overhead for searching for an empty one (in the case that the user only added and never removed for 500 items) counter balance any resource issues?I don't really know how JavaScript arrays work in memory and such.

Share this post


Link to post
Share on other sites

nm

Edited by thescientist

Share this post


Link to post
Share on other sites

Unless you're getting into significant numbers of array elements (maybe 50,000+) and/or huge amounts of data in each element, I would not worry. If the content of an array element is null, then you only have a few bytes on a lookup table that point nowhere.Try running this thing on an old browser on an old system on an old processor and see what kind of degradation you notice. That's the only kind of test that matters.

Share this post


Link to post
Share on other sites

Alright, the oldest machine I own is an XP machine from 2005 with a Pentium D in it. Is that old enough?

Share this post


Link to post
Share on other sites

Sounds reasonable. Now jack up your array till it's 10 times as large as it's ever likely to get. What's the performance hit?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...