RubyGuides
Share this post!

Performance Improvements in Ruby 2.5

New Ruby versions keep improving & Ruby 2.5 is no different.

Ruby 2.5 is introducing some optimizations:

  • String interpolation will be around 72% faster when a large string is created
  • String#prepend will be around 42% faster if only one argument is given
  • Enumerable#sort_by, Enumerable#min_by & Enumerable#max_by will be about 50% faster

Let's see some benchmarks!

String Interpolation Performance

I took the example code in the commit message for this optimization:

I ran this benchmark with the following results.

Ruby 2.4.1:

Ruby 2.5:

As you can see the difference for large strings is pretty remarkable!

String#prepend Performance

The prepend method allows you to insert some text in front of an array.

Ruby 2.5 optimizes for the most common case, prepending only one string to another.

Here are the benchmark results.

Ruby 2.4.1 results:

Ruby 2.5 results:

That's a pretty good improvement!

Enumerable Performance Improvements

A few Enumerable methods are getting a performance upgrade.

This particular optimization works because it skips method dispatching for the <=> method.

As described on the commit message:

"Use OPTIMIZED_CMP() to compare the objects instead of <=> method dispatching for Fixnum/Float/String object."

Here are my benchmark results.

Ruby 2.4.2:

Ruby 2.5:

That's about a 50% improvement 🙂

Range#min & Range#max

I have two bonus performance optimizations for you!

One is about the Range#min & Range#max methods.

Here are the benchmarks:

Ruby 2.4.2

Ruby 2.5

Find the commit here.

Improved String#scan

According to the commit message this improves performance by 50% for a string pattern & 10% for a regex pattern.

Let’s look at the benchmarks!

Ruby 2.4.2

Ruby 2.5

Happy faster scanning!

Summary

You learned about the new optimizations coming in Ruby 2.5, to be released on December 25.

These optimizations are about string interpolation, Enumerable methods, the String#prepend method, the String#scan method & the Range#max / Range#mix methods.

I hope you enjoyed this post!

Don't forget to share the post on your favorite social networks 🙂

Leave a Comment:

8 comments
Ilya Vorontso says last month

According to your benchmark, String#scan is slower in ruby-2.5. Is it a mistake?

Reply
    Jesus Castello says last month

    Fixed. Sorry about that 🙂

    Reply
Adam D says last month

Looks great! One thing… is the last example backwards?

Cheers!

Reply
    Jesus Castello says last month

    Yes. It’s fixed now 🙂

    Reply
hachi8833 says last month

I’d like to translate the article and publish on our blog if you’re OK.
I will make sure to indicate the link to original, author name, and your site.

Best regards,

Reply
Add Your Reply