Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Move an array element
(version: 0)
Move an array element
Comparing performance of:
splice vs flatMap vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
splice
flatMap
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
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/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
splice
8027132.5 Ops/sec
flatMap
5230823.5 Ops/sec
reduce
7343710.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **Benchmark Definition** The benchmark defines three test cases: `splice`, `flatMap`, and `reduce`. Each test case has a similar structure: 1. A JavaScript function is defined to move an element from one index to another in an array. 2. The function takes two parameters: the original array, the old index of the element to be moved, and the new index where the element should be inserted. **Options Compared** The benchmark compares three different approaches for moving an element in an array: 1. **Splice**: Uses `arr.splice(newIndex, 0, arr[oldIndex])` to insert the element at the new index. 2. **flatMap**: Uses `arr.flatMap((item, index) => {...})` to create a new array with the element moved. 3. **Reduce**: Uses `arr.reduce((acc, item, index) => {...})` to accumulate the elements of the original array and insert the moved element at the correct position. **Pros and Cons** Here's a brief summary of each approach: * **Splice**: + Pros: Simple and efficient for small arrays. + Cons: May not be suitable for large arrays due to its overhead in searching and inserting elements. * **flatMap**: + Pros: Can handle large arrays efficiently, as it avoids the need to search for the element. + Cons: Creates a new array, which may lead to memory allocation issues for very large datasets. * **Reduce**: + Pros: Handles large arrays well and can be more efficient than `splice` for small to medium-sized arrays. + Cons: Requires an accumulator variable, which can be slower than the other two approaches. **Library** There is no explicit library mentioned in the benchmark definition. However, `flatMap` is a method of the `Array.prototype` object in JavaScript, so it doesn't require any external libraries. **Special JS Feature or Syntax** None of the test cases use any special JavaScript features or syntax beyond standard ECMAScript 2022.
Related benchmarks:
Array clone
SpreadVSPush
Move item in first place spread vs unshift
Move item in first place unshift vs spread
Clone Array - 08/02/2024
Comments
Confirm delete:
Do you really want to delete benchmark?