0

I usually have if/else conditions which involves comparing values with a constant string.

Is it really advantageous to use symbols in such cases or use string. For eg.

if status == 'submitted'
  ...
elsif status.try(:to_sym) == :open
  ...

Also what are the affects on memory consumption when this condition is in an instance method or class method or global method. Is the string takes up memory every time it's called ?

2 Answers 2

1

Strings and Symbols are fundamentally different. String is a datatype for text. Symbols are a datatype for unique identifiers. You should use the one which best models your problem domain.

There may or may not be performance differences, but as always, you should only start thinking about exchanging strings for symbols, if all of the following are true:

  • you have a hard performance requirement
  • you have a precise definition of that hard performance requirement
  • you have an exact number to hit for that hard performance requirement
  • you have a statistically significant, representative, repeatable, automated benchmark for that performance requirement
  • running that benchmark shows that you don't meet your performance requirement
  • you cannot switch on more aggressive optimizations
  • you cannot switch to a faster Ruby implementation
  • you cannot buy faster hardware
  • you cannot buy bigger hardware
  • you cannot buy more hardware
  • you cannot tweak the existing architecture
  • you cannot change to a more performant architecture
  • you cannot tweak the existing design
  • you cannot change to a more performant design
  • you cannot tweak the existing algorithm
  • you cannot change to a more performant algorithm
  • you have statistically significant, representative profiling data that shows that string comparisons are indeed the most pressing bottleneck in your application

Then, and only then does it make sense to think about such micro-optimizations.

Note that with Ruby moving towards immutable strings and string deduplication on the one hand, and garbage-collectable symbols on the other hand, the performance differences are likely to get smaller. It really is all about semantics: if it's text, use a string, if it's a unique name, use a symbol.

1

In Ruby, symbols and string are the same with only one difference. Symbols are inmutable. here

Because of the strings are mutable, it also has a performance impact.

But IMHO, I think the most important reason to use a symbol instead of a string can be found in this article:

"... to use symbols every time you need internal identifiers. ..."

And in your particular case, you have indeed an internal identifier for status. Future developers of your code will understand that you are refering to a particular state and not to the representation you have at the moment.

2
  • Thanks that helps a lot. One more point I pondered upon is that the status comes as a param to the api so it's always as a string so that condition is a bit different, it's something like elsif status.try(:to_sym) == :open. I see that for ruby < 2.2 that is not a good option, however let's say if there are only two status. Then the memory of those two symbols only will be allocated. Commented Nov 24, 2015 at 12:41
  • Again, I don't think you should focus too much on the performance side, but more on what you intent is. If you only have two different status, then probably your performance gain or loss may be irrelevant. Commented Nov 24, 2015 at 13:47

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.