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 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 252k
  • Answers 252k
  • 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
  • Editorial Team
    Editorial Team added an answer IDisposable and thus Dispose is not used to reduced memory… May 13, 2026 at 9:39 am
  • Editorial Team
    Editorial Team added an answer From tablesorter.com $(document).ready(function() { $("table").tablesorter(); $("#trigger-link").click(function() { // set sorting… May 13, 2026 at 9:39 am
  • Editorial Team
    Editorial Team added an answer You could set up a RESTful API that can speak… May 13, 2026 at 9:39 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

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

Top Members

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.