Project

General

Profile

Actions

Patch #40008

closed

Replace String#sub with delete_prefix / delete_suffix

Added by Go MAEDA 12 months ago. Updated 12 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

The attached patch replaces `String#sub` which is used to remove leading or trailing substring with String#delete_prefix and String#delete_suffix introduced in Ruby 2.5.

The change improves the efficiency and readability of the code.

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
'#123'.delete_prefix('#')
                         1.497M i/100ms
'#123'.sub('^#', '')   936.977k i/100ms
Calculating -------------------------------------
'#123'.delete_prefix('#')
                         14.822M (± 0.5%) i/s -     74.837M in   5.049042s
'#123'.sub('^#', '')      9.322M (± 1.2%) i/s -     46.849M in   5.026072s

Comparison:
'#123'.delete_prefix('#'): 14822395.1 i/s
'#123'.sub('^#', ''):  9322431.1 i/s - 1.59x  slower

Files

Actions #2

Updated by Go MAEDA 12 months ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patch in r22596.

Actions

Also available in: Atom PDF