Ruby symbols are not garbage collected!? Then, isn't it better to use a String?

前端 未结 3 1303
一个人的身影
一个人的身影 2021-02-14 14:14

If you create 10,000 strings in a loop, a lot of garbage collection has to take place which uses up a lot of resources.

If you do the same thing with symbols, you create

3条回答
  •  天涯浪人
    2021-02-14 15:08

    Seeing as symbols are almost always created via literals, there isn't much potential for a memory explosion here. Their behavior is pretty much required by their usage: every time you refer to a symbol, it's the same one.

    Similarly, strings need to be unique in Ruby. This is due to the way they're used - text processing etc.

    Decide which one to use depending on their semantics, don't optimize prematurely.

提交回复
热议问题