-1

I have array visitors with 1000 records.

In this array I look for and element by _found.idVisitor:

  let index = this.visitors.findIndex(
          st => st.idVisitor === _found.idVisitor
        );


  this.visitors.splice(index, 1);
  this.visitors.unshift(_found);

Then I pull this element by index and push to the top of array?

But this approach take long time, how to optimize it?

Dipak Telangre
  • 1,792
  • 4
  • 19
  • 46

1 Answers1

0

Maybe this can help you:

Why is Array.splice() so slow?

Heres my approach to the Problem:

var deleteAtIndex = function(arr, index) 
{
    var len = arr.length;

    if (!len) 
    { 
        return ;
    }

    while (index<len) 
    { 
        arr[index] = arr[index+1]; 
        index++;
    }
    arr.length--;
};

this just rearranges the array and is way faster for me.

(i think the splice function creates copies of the array when rearranging it like an ArrayList in Java does which makes it slower - but these are just my 2 cents)