I have an array of strings that is loaded from the server. The array may be quite big and contains many elements. I need to have a filter and pagination functionality.
To achieve pagination on filtered data I need to store the filtered data somewhere first. Is it possible that the filter array contains only references to items in the original data to avoid an additional memory overhead? If I just duplicate the original array, the already big memory footprint will be doubled initially and possibly shrink when the data will be filtered. If I'll have an array with pointers (the original array won't change) I could make it more memory efficient.
Asked
Active
Viewed 69 times
-2

Djent
- 2,877
- 10
- 41
- 66
-
Keep an array of indexes? – VLAZ Jun 15 '22 at 06:02
-
Yeah, I was thinking about that, but that seems a bit clunky. I was thinking if there's something better. – Djent Jun 15 '22 at 06:06
-
You could also wrap your data in objects and just keep those around if you wish. Not sure it's any less or more clunky than having indexes. – VLAZ Jun 15 '22 at 06:08
-
Javascript objects are reference counted, meaning that both original and filtered array already store and reuse references. If your original array elements or their order do not change during pagination, or it's immutable, to efficiently utilize memory you can create bitset with TypedArray. TypedArray takes less memory than array with indices. – Ruslan Tushov Jun 15 '22 at 06:09
-
I was thinking, perhaps there is a npm package or just different array type that does that under the hood automatically. – Djent Jun 15 '22 at 06:09
-
Good JavaScript engines will already optimise this: the *same* string is not stored twice in memory. See [string interning](https://stackoverflow.com/questions/5276915/do-common-javascript-implementations-use-string-interning) – trincot Jun 15 '22 at 06:11
-
Oh, I didn't know about it. Then it's done automatically under the hood and the optimizations are not necessary. Thanks @trincot. You may post it as an answer so it'll be accepted. – Djent Jun 15 '22 at 06:12
1 Answers
0
Actually, what you want is exactly how things work in Javascript
const list = [objA, objB, objC];
const filteredList = list.filter((o, index) => index === 1));
filteredList[0] === list[1] // identity: true
filteredList[0] === objB // identity: true
The array holds references to the objects. These will not be cloned by actions like filter

Arjan
- 1,034
- 8
- 29