Jump to content

Best practice for ever changing arrays.


shadowayex

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.

Link to comment
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.
Link to comment
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.

Link to comment
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.
Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...