Move array element to the first position I wrote an update() function to make an entry the first element in an array.

var arr = [
    {v: 1},
    {v: 2},
    {v: 3},
    {v: 4},
];

function update(e) {
    var i = arr.findIndex(o =&gt; o.v === e.v);
    if (i === 0) return;
    if (i &gt; 0) arr = [e].concat(arr.slice(0, i), arr.slice(i+1));
    if (i &lt; 0) arr = [e].concat(arr.slice(0, arr.length - 1)); // keep the array has 4 elements
}

update({v: 1});
update({v: 2});
update({v: 6});


Is there any better way to do this? I think slice and concat will make the performance not good.
function update(e) {

    // find index
    var i = arr.findIndex(o =&gt; o.v === e.v);

    // if already at start, nothing to do
    if (i === 0) return;

    // remove old occurrency, if existing
    if (i &gt; 0) {
        arr.splice( i, 1 );
    }

    // add e to the start
    arr.unshift( e );

    // keep array at the correct size
    arr.length = Math.min( arr.length, 4 );
}


Somewhat less code. I don't know about the performance.