HTML Preparation code:
AخA
 
1
<div id='1'></div>
Script Preparation code:
 
var arr = [1,2,3,4];
Tests:
  • splice

    x
     
    function arrayMove(arr, oldIndex, newIndex) {
      const copiedArr = [...arr];
      const length = copiedArr.length;
      
      if (oldIndex !== newIndex && length > oldIndex && length > newIndex) {
        copiedArr.splice(newIndex, 0, copiedArr.splice(oldIndex, 1)[0]);
      }
      
      return copiedArr;
    }
    arrayMove(arr, 0, 3)
  • flatMap

     
    function arrayMove(arr, oldIndex, newIndex) {
        const length = arr.length;
        const itemToMove = arr[oldIndex]
        if (oldIndex === newIndex || oldIndex > length || newIndex > length) {
            return arr;
        }
        return arr.flatMap((item, index) => {
            if (index === oldIndex) return [];
            if (index === newIndex) return oldIndex < newIndex ? [item, itemToMove] : [itemToMove, item];
            return item;
        })
    }
    arrayMove(arr, 0, 3)
  • reduce

     
    function arrayMove(arr, oldIndex, newIndex) {
        const length = arr.length;
        const itemToMove = arr[oldIndex]
        if (oldIndex === newIndex || oldIndex > length || newIndex > length) {
            return arr;
        }
        return arr.reduce((acc, item, index) => {
            if (index === oldIndex) return acc;
            if (index === newIndex) return oldIndex < newIndex ? [...acc, item, itemToMove] : [...acc, itemToMove, item];
            return [...acc, item];
        }, [])
    }
    arrayMove(arr, 0, 3)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    splice
    flatMap
    reduce

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.20
Chrome 108 on Windows
View result in a separate tab
Test name Executions per second
splice 5010550.5 Ops/sec
flatMap 952172.3 Ops/sec
reduce 4747937.0 Ops/sec