Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:05:33+00:00 2026-05-30T22:05:33+00:00

Which is the faster way (in character level) when checking if two strings or

  • 0

Which is the faster way (in character level) when checking if two strings or RegExp match exactly? If the string is extremely long or have to check many times?

  1. str == str_or_regexp || str =~ str_or_regexp

  2. str[str_or_regexp] == str

Or there is better way?

We don’t know str_or_regexp is a string or a regular expression until runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:05:34+00:00Added an answer on May 30, 2026 at 10:05 pm

    When all else fails, run some tests:

    str = 'string'
    s = 'string'
    r = /string/
    
    methods = {
      :equals_or_matches => lambda { |t| str == t || str =~ t },
      :square_brackets => lambda { |t| str[t] == str }
    }
    
    methods.each_pair do |name, method|
      puts name
    
      [s, r].each do |t|
        puts t.class
    
        5.times do
          start = Time.now
          1000000.times do
            method.call(t)
          end
          puts Time.now - start
        end
      end
    
      puts
    end
    

    I got these results:

    equals_or_matches
    String
    0.942799
    0.942405
    0.944376
    0.946296
    0.93843
    Regexp
    1.916263
    1.915058
    1.913306
    1.934423
    1.932633
    
    square_brackets
    String
    1.15087
    1.157245
    1.157863
    1.174356
    1.188758
    Regexp
    2.09721
    2.103493
    2.028035
    2.025194
    2.037734
    

    That indicates that your first method is a little bit faster than your second.

    However, if the strings are not equal and the || doesn’t “short circuit”, you’ll get a TypeError. You can’t pass a string to =~. So you should probably replaced that with str.match(t), which gave me these results:

    equals_or_matches
    String
    0.936063
    0.94154
    0.938561
    0.934187
    0.935868
    Regexp
    2.755815
    2.75011
    2.758374
    2.761684
    2.76826
    
    square_brackets
    String
    1.198433
    1.160929
    1.354407
    1.410265
    1.274158
    Regexp
    2.013017
    2.275579
    2.297108
    2.165399
    2.125889
    

    In that case, your first method fared much worse for regexps, but the second was about the same.

    Like I said, just run some tests on real data and see what happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two way of doing things and I'm curious which is faster: First way: if
I am wondering which way would end up being faster, selecting elements by: $('element[href=#my_link]');
What's the fastest way to parse strings in C#? Currently I'm just using string
I'm trying to implement a simple search function. I have a string array which
Well friends, I have got this query which works but is very long for
I have a customer table, which has a field categories. It's a string holding
Which is faster? someCondition has the same probability of being true as it has
Which is faster: Union or Concat ? I don't care about the order of
Just wondering which is faster: DELETE FROM table_name WHERE X='1' DELETE FROM table_name WHERE
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.