I'm just starting out with solidity and right from the gecko am met with several ways to store strings i.e

    string _data = "MyString";

    string private _data = "MyString";

    string internal _data = "MyString";

    bytes8 private constant = "MyString";

And various combinations of above. I am trying to understand how these affect gas costs and so far am pretty confident that

  1. Using bytesX is better than string where possible
  2. Adding constant is better when value won't be changing
  3. Using private is better than internal if this value is accessible only in this contract

I wanted to ask if my assumptions above are correct, if same applies to arrays i.e. bytes8[2] = ["MyString", "XyString"] and is there anything else I need to be aware of i.e. when to use memory?

2 Answers 2


The difference can be explained by how the compiler implement those types.

bytesNN is fixed sequence of NN bytes while string is a dynamic array of bytes. Using string increases the runtime bytecode due to a few auxiliary function included by the compiler.

For a public variable the compiler has to create a getter function.

A constant declaration will be inlined by the compiler, it will not use storage.

For example bytes8[2] is a fixed array of 2 bytes8.

  • Could you please expand on constant? As I am not really getting it, it will still be accessible i.e. if I say save some string to re-use latter, so I assume it has to be stored?
    – Ilja
    Oct 23, 2021 at 14:39
  • 1
    @Ilja A constant string will continue working as a string as it is expected. It will not use storage slots, it will be stored together with the contract bytecode.
    – Ismael
    Oct 23, 2021 at 16:52

i suppose the more restrictive it is, the more it is efficient, hence the reason why private would be more efficient than internal ?

EDIT : i mean the resulted code once compiled would be more efficient, at least that's what i'm thinking and makes sense to me, correct me if i'm wrong.

  • It is not how narrow the scope it is but how it is implemented by the compiler.
    – Ismael
    Oct 23, 2021 at 3:23

Your Answer

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

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