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 9214593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:00:14+00:00 2026-06-18T02:00:14+00:00

I have the following code: var select_ids = []; // syntax.filter.select_list is [elem1, elem2]

  • 0

I have the following code:

var select_ids = [];
// syntax.filter.select_list is ["elem1", "elem2"]
for (var o = 0; o < syntax.filter.select_list.length; o += 1) {
    element = syntax.filter.select_list[o];
    if (priv.getPositionInArray(element,select_ids) === null ) {
      // this line hangs up the browser
      select_ids.unshift(element);
    }
}

I don’t understand why push() or unshift() into an empty array when looping two iterations hangs up the browser for 3-4sec.

If I leave away this line:

select_ids.unshift(element);

the script runs instantly, so

Question:
why could push/unshift to an empty array produce such a delay? FYI, this is inside another loop with 3 iterations. For every parent loop I’m resetting select_ids to an empty object to fill.

Thanks!

EDIT:
And I’m doing the if-clause to make sure I don’t add doubles to the array.

EDIT: (full code)

priv.findBestIndexForQuery = function (indices, syntax) {
    var i, j, k, l, m, n, p, o, element,
      search_ids = [], select_ids = [], use_index = [],
      index, query_param, search_param,
      // need to parse into object
      current_query = jIO.ComplexQueries.parse(syntax.query);

    // loop indices
    for (i = 0; i < priv.indices.length; i += 1) {
      index = {};
      index.reference = priv.indices[i];
      index.reference_size = index.reference.fields.length;

      // rebuild for iteration
      if (current_query.query_list === undefined) {
        search_ids.push(current_query.id);
      } else {
        for (j = 0; j < current_query.query_list.length; j += 1) {
            if (priv.getPositionInArray(current_query.query_list[j].id, 
                search_ids) === null ) {
                search_ids.push(current_query.query_list[j].id);
            }
        }
      }
      for (o = 0; o < syntax.filter.select_list.length; o += 1) {
         element = syntax.filter.select_list[o];
         if (priv.getPositionInArray(element,select_ids) === null ) {
              // line causing problems
              select_ids.unshift(element);
         }
      }
// there is a lot more, but I'm hanging on the line above
}

EDIT (getPostionInArray)

  priv.getPositionInArray = function (element, array) {
     var i;
     for (i = 0; i < array.length; i += 1) {
        if (array[i] === element) {
           return i;
        }
     }
     return null;
  };
  • 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-06-18T02:00:16+00:00Added an answer on June 18, 2026 at 2:00 am

    As requested by the OP, the issue is, indeed unshift: there have been bugs with this function (its context is ad-hoc) in the past, and this bug report is still marked as NEW.

    As for your getPositionInArray function, why don’t you use the standard indexOf method? Most browsers support it, and for those that don’t you can always add this to your script:

    if (!Array.prototype.indexOf)
    {
        Array.prototype.indexOf = function(search)
        {
            var i;
            for (i = 0; i < this.length; i++)
            {
                if (this[i] === search)
                {//I use type & value check...
                    return i;
                }
            }
            return -1;//-1 is the standard behaviour
        };
    }
    

    Or, if you want this method to return null, instead of -1 in all browsers:

    Array.prototype.indexOf = (function (nativeFunc)
    {
        return function(search)
        {
            var retVal = nativeFunc.apply(this,[search]);//use native method
            return (retVal === -1 ? null : retVal);//return null if the native method returned -1
        };
    }(Array.prototype.indexOf));//pass initial method to IIFE
    

    Just a side-note, some prototype fun 🙂

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

Sidebar

Related Questions

I Have following code with LINQ var q = (from web in DataContext.Webs select
I currently have the following js code function clearMulti(option) { var i; var select
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var refreshId = setInterval(function() { $(#footad).html('myjshere'); }, 15500); Where
I have the following code: var carsContext = new CarsDataContext(); IQueryable<Car> allCars = carsContext.Cars;
I have the following code var dbvalue = Ned; var deleteLink = '<a href=#delete
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var bool:String = true; Without an if block or
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal

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.