1

I have a very large array of objects (nested objects).

var array = [
    { a: "a", b: { .. }, c:"c", ... },
    {...}
]

After some API call, I get a brand new array with 1 modified element and I know exactly which element is modified.

  1. Is it a good idea (in terms of memory usage and performance) to assign array with the new value or replace only the modified object.

  2. Do I need to modify the API to only send the modified object and update the array accordingly?

The API is developed by another team and it transfers a huge amount of data on each request. I need a solid technical answer to convince them to change the API to send only the required data and do modification at the client side.

3
  • in terms of memory, both are already in memory. In terms of perf, reassigning is basically free, while modifying requires to search for what to modify. What your API should return also depends on the shape of the API calls. whatever you do, the API should remain logic, consistent and predictable. Commented Oct 19, 2016 at 17:46
  • Sending only the modified element from the API might not make performance better, but it can definitely improve bandwidth if the API is hosted on a remote server. Commented Oct 19, 2016 at 17:49
  • I would prefer only to get the modified object from the server and dynamically replacing it with the obtained JSON data. Commented Oct 19, 2016 at 17:52

1 Answer 1

2

If by

"I know exactly which element is modified"

you mean you know the exact position of the modified element, then replacing it is an O(1) operation:

array[positionOfModified] = modified;

Otherwise, you will have to find the element, which is usually an O(N) operation unless you do something like a binary search if the array is sorted (O(logN)).

Therefore, in terms of speed, it could potentially be slower to replace the modified object then to just replace array references:

array = newArray;

However, the space (memory) improvement would likely be much larger than the possible speed regression.

Returning only the modified element will reduce your bandwidth since you'll be sending a single object instead of a large array. If this request happens frequently (many users requesting many times, possibly simultaneously), by returning the whole array every time, you are risking congesting your network.

The application memory usage will also be improved because you'll be overwriting a single object instead of an array, thus the garbage collector will only have to worry about cleaning up the modified object, not the entire previous array. Replacing references of large arrays, especially if this replacement is done often (maybe faster than the GC does its cleanup cycles), could blow up your memory fairly quickly.

Ideally, what you could do is send the modified object and its position in the array back, something like:

{
  element: { ... }
  position: ...
}

this will allow you to use small memory/bandwidth while keeping the update process a constant operation.

array[response.position] = response.element;
Sign up to request clarification or add additional context in comments.

2 Comments

Surely i can do array[position] = newobject as i know the position. But array = newarrayis also O(1)?
any reference assignment is an O(1) operation, a = 3, b = {}, c = [], etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.