Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Move an array element
Move an array element
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser:
Chrome 135
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
splice
8027132.5 Ops/sec
flatMap
5230823.5 Ops/sec
reduce
7343710.0 Ops/sec
HTML Preparation code:
<div id='1'></div>
Script Preparation code:
var arr = [1,2,3,4];
Tests:
splice
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)