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

  • Home
  • SEARCH
  • 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 51159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:42:24+00:00 2026-05-10T16:42:24+00:00

I’ve recently come across this piece of JavaScript code: if (,>=,<=,<>,.indexOf(, + sCompOp +

  • 0

I’ve recently come across this piece of JavaScript code:

if (',>=,<=,<>,'.indexOf(',' + sCompOp + ',') != -1) 

I was intrigued, because to write this test I would have done:

if (/(>=|<=|<>)/.test(sCompOp)) 

Is this just a stylistic difference, or does the author of the other code know something about optimization that I don’t? Or perhaps there is a different good reason to do this, or to not use regexes…?

It seems to me that using String.indexOf() for this is a little more difficult to read (but then, I’m quite comfortable with regular expressions), but are there instances where it might be ‘better’ than writing an equivalent regex?

By ‘better’ that might be quicker or more efficient, (although obviously that depends on the browser’s JavaScript engine), or some other reason I’m not aware of. Can anyone enlighten me?

  • 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. 2026-05-10T16:42:25+00:00Added an answer on May 10, 2026 at 4:42 pm

    I ran some tests. The first method is slightly faster, but not by enough to make any real difference even under heavy use… except when sCompOp could potentially be a very long string. Because the first method searches a fixed-length string, its execution time is very stable no matter how long sCompOp gets, while the second method will potentially iterate through the entire length of sCompOp.

    Also, the second method will potentially match invalid strings – "blah blah blah <= blah blah" satisfies the test…

    Given that you’re likely doing the work of parsing out the operator elsewhere, i doubt either edge case would be a problem. But even if this were not the case, a small modification to the expression would resolve both issues:

    /^(>=|<=|<>)$/ 

    Testing code:

    function Time(fn, iter) {    var start = new Date();    for (var i=0; i<iter; ++i)       fn();    var end = new Date();    console.log(fn.toString().replace(/[\r|\n]/g, ' '), "\n : " + (end-start)); }  function IndexMethod(op) {    return (",>=,<=,<>,".indexOf("," + op + ",") != -1); }  function RegexMethod(op) {    return /(>=|<=|<>)/.test(op); }  function timeTests() {    var loopCount = 50000;        Time(function(){IndexMethod(">=");}, loopCount);    Time(function(){IndexMethod("<=");}, loopCount);    Time(function(){IndexMethod("<>");}, loopCount);    Time(function(){IndexMethod("!!");}, loopCount);    Time(function(){IndexMethod("the quick brown foxes jumped over the lazy dogs");}, loopCount);    Time(function(){IndexMethod("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");}, loopCount);     Time(function(){RegexMethod(">=");}, loopCount);    Time(function(){RegexMethod("<=");}, loopCount);    Time(function(){RegexMethod("<>");}, loopCount);    Time(function(){RegexMethod("!!");}, loopCount);    Time(function(){RegexMethod("the quick brown foxes jumped over the lazy dogs");}, loopCount);    Time(function(){RegexMethod("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");}, loopCount); }  timeTests(); 

    Tested in IE6, FF3, Chrome 0.2.149.30

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

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer A machine that is hibernating cannot come out of sleep… May 11, 2026 at 6:36 am
  • added an answer Using subdomains for content elements isn't so much to force… May 11, 2026 at 6:36 am
  • added an answer It was on www.churchillobjects.com, but that domain has now been… May 11, 2026 at 6:36 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.