Ruby has a lot of methods available for the
String class for the various use cases, and one of them is to freeze/unfreeze string.
Two commonly used methods for unfreezing string literals are
String#+(unary plus) and
While both of them can be used to unfreeze the string
String#+ was preferred over
because it’s faster than the latter.
In versions before 3.3
String#+ was almost two times faster than the much easier to read and convenient
The performance improvement comes from the fact that
String#+@ can directly go through
rb_str_dup (a specific method for duplicating strings) instead of using the more generic and slower
rb_obj_dup (a more general method for duplicating objects).
We can compare the performance metrics of both methods by using
From the benchmark test, we can observe that the Unary plus method is faster as compared to
It also led to the
which encourages the performance-oriented developers to prefer
String#+ over much
From Ruby 3.3 onwards, the
String.dup has been optimized to be as efficient as
It means that now developers can choose the method based on readability and preference without sacrificing performance.
We can confirm the same by performing a benchmark test with Ruby version 3.3
From the above benchmark test it is confirmed that the
String.dup is now as fast as
To know more about this, please refer to this PR.