2

In most articles they recommend to use mapping. But mappings don't allow to iterate their items directly. And nobody says what to do if you need to get all the stored items.

In my case I need store any items and then display them in web like cryptokitties. Note: they are able to dislay all the items.

So what should be better - storing all the values as array or storing all the values as mapping and store ids for them as array? And what case is better for "paging"? Maybe are there other solutions?

1

Storing all values as mapping and store ids of them in an array is better. As with the help of mapping, you can access individual record directly using the key. Worst case Time complexity of accessing individual record is O(1). However, if you use an array to access/search specific record then you will have to iterate the complete array and worst case time complexity will be O(N). You will have to iterate over all the elements stored in an array. (I am assuming id's of records stored are different from array indices).

If you store keys of mapping in an array you will be able to retrieve all the records using keys stored in an array. By using those keys you can get all the records.

Refer the following blog for more details -

https://blog.upstate.agency/mappings-arrays-87afc697e64f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.