Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Move element inside array (v3)
(version: 2)
Comparing performance of:
Universal move to start vs Universal move to end vs Move to start vs Move to end vs Universal move to start (new array) vs Universal move to end (new array)
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const arr = [] for (let i = 1; i <= 100000; i++) { arr.push("element " + i) } function move1(from, to) { arr.splice(to, 0, arr.splice(from, 1)[0]); } function move2(from) { return [ arr[from], ...arr.filter((el, i) => i !== from) ] } function move3(from) { return [ ...arr.filter((el, i) => i !== from), arr[from] ] } function move4(from, to) { const testArr = arr.slice(0) testArr.splice(to, 0, testArr.splice(from, 1)[0]) return testArr }
Tests:
Universal move to start
for (let i = 1; i <= 1000; i++) { move1(4999, 0) }
Universal move to end
for (let i = 1; i <= 1000; i++) { move1(4999, 0) }
Move to start
for (let i = 1; i <= 1000; i++) { move2(4999) }
Move to end
for (let i = 1; i <= 1000; i++) { move3(4999) }
Universal move to start (new array)
for (let i = 1; i <= 1000; i++) { move4(4999, 0) }
Universal move to end (new array)
for (let i = 1; i <= 1000; i++) { move4(4999, 100000 - 1) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Universal move to start
Universal move to end
Move to start
Move to end
Universal move to start (new array)
Universal move to end (new array)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its various components. **Benchmark Overview** The benchmark measures the performance of three different approaches for moving an element inside an array in JavaScript. The array contains 100,000 elements, and the test case iterates 1,000 times to ensure a good representative sample. **Approaches Compared** There are four approaches compared: 1. **Move1**: This approach uses `splice()` to move the element from one index to another. It first removes the element at the destination index and then inserts it at the source index. 2. **Move2**: This approach uses array destructuring to create a new array with the element moved to the beginning of the array. The rest of the elements are removed in the process. 3. **Move3**: Similar to Move2, this approach also uses array destructuring to move the element to the beginning of the array, but it removes all elements after the source index instead of just removing them and inserting the moved element at the destination index. 4. **Move4**: This approach creates a new array by copying the original array and then using `splice()` to move the element. **Pros and Cons** 1. **Move1**: * Pros: Simple implementation, efficient in terms of memory usage. * Cons: May be slower than other approaches due to repeated calls to `splice()`. 2. **Move2**: * Pros: Efficient in terms of time complexity, uses array destructuring for a concise solution. * Cons: Requires JavaScript version 3 or higher, may not work well with older browsers. 3. **Move3**: * Pros: Similar efficiency to Move2, but also works on older browsers that don't support array destructuring. * Cons: More complex implementation than Move1 and Move4. 4. **Move4**: * Pros: Simple implementation, efficient in terms of memory usage. * Cons: May be slower than other approaches due to the creation of a new array. **Library Used** The benchmark uses JavaScript's built-in `splice()` function and array destructuring feature (introduced in ECMAScript 2015). **Special JS Feature/Syntax** None mentioned in this explanation. However, it's worth noting that the benchmark requires JavaScript version 3 or higher to run the Move2 and Move3 approaches. **Alternatives** Other approaches for moving an element inside an array could include: * Using `array.prototype.forEach()` and indexing into the array * Creating a new array using `Array.from()` and then modifying it * Using `Array.prototype.slice()` and concatenation However, these alternatives might not be as efficient or concise as the approaches used in the benchmark.
Related benchmarks:
speed of array splice and filter
comparing Array.from copy and then splice with filter method
Slice vs Splice vs Shift (Large Array)
Splice vs shift to remove at beginning of array (fixed from slice)
Splice vs Shift to remove from the beginning
Comments
Confirm delete:
Do you really want to delete benchmark?