When storing values in mappings in solidity, will it take the same storage size for uint8 and uint16? I.E all uint's take up the same storage space?


I can't say for storage space, but I tried measuring gas usage, and uint256 actually used less gas to push values onto a mapping than uint8. Using the following contract, uint256 cost 46707 gas and uint8 cost 46944 gas. Reading also cost less for uint256.

This is the contract I used to test:

pragma solidity ^0.4.21;

contract Test {

    mapping(int => uint8) one;
    mapping(int => uint256) two;
    int counter = 0;

    function pushOne() public {
        one[counter++] = 1;

    function pushTwo() public {
        two[counter++] = 256;

| improve this answer | |
  • 1
    It's costs more for smaller types because when you use a smaller sized uint, solidity does an extra bitwise & to clear the top bits. – Tjaden Hess Oct 3 '18 at 20:29

It depends how you do it.

If you do

mapping(bytes32 = uint8) tinyInts;
mapping(bytes32 => uint16) biggerInts;

It will cost more than

struct IntStruct {
  uint8 tinyInt;
  uint16 biggerInt;

mapping(bytes32 => IntStruct) intStructs;

... because a 32-byte word is the smallest addressable slot in storage. In the first instance, there are two different references to two different 32-byte words (so 64 bytes, total). Indeed, both values could be uint256 precision and this would actually be marginally cheaper because of the extra work involved in packing/unpacking lower precision numbers.

In the second, a single 32-byte word would accommodate both values with room to spare in a 32-byte word. Since storage the biggest expense, the more compact structure is cheaper.

Mappings point to objects that are composed of 32-byte words in all cases.

Hope it helps.

| improve this answer | |

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.