3 ms·
> as of Ruby 2.2, there are zero-performance gains using symbols over strings As others have pointed out - this is demonstrably false. # frozen_string_lit
by chrisseaton 8y ago
> as of Ruby 2.2, there are zero-performance gains using symbols over strings
As others have pointed out - this is demonstrably false.
# frozen_string_literal: true
a = ('fo' + 'o').to_sym
:foo == a
b = ('fo' + 'o').freeze
'foo' == b
The second comparison is a 1.5x slower than the former. This is because if a string comparison fails on identity, it then has to check characters. Symbols are always interned so can always stop as the identity comparison.
But I actually just like them as they convey intent - a symbol is a string I know about as the programmer. A string is a string that is runtime data.
- jaequery 8y agoI too like it as you. But I find when dealing with a lot of JSON data (decoding/encoding) the symbols become a hindrance.
- deleted 8y ago[deleted]