Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Some reorder test (not reverse)
(version: 0)
Testing reordering of elements in array.
Comparing performance of:
splice (non-mutating) vs spread vs concat
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = []; for (let i = 0; i < 10000; i++) { list.push(i); }
Tests:
splice (non-mutating)
for(let i = 0; i < 5000; i++) { const result = Array.from(list); const [removed] = result.splice(9000, 1); result.splice(i, 0, removed); list = result; }
spread
for(let i = 0; i < 5000; i++) { list = [list[9000], ...list.slice(0, 9000), ...list.slice(9001)]; }
concat
for(let i = 0; i < 5000; i++) { list = [list[9000]].concat(list.slice(0,9000)).concat(list.slice(9001)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
splice (non-mutating)
spread
concat
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):
The provided JSON represents a JavaScript benchmark test case, and it's designed to measure the performance of reordering elements in an array. **What is tested:** The benchmark tests three different approaches to reorder the last element of an array: 1. **Splice (non-mutating)**: This approach uses the `splice` method with two arguments to remove the last element and then insert it at a specified position. 2. **Spread**: This approach uses the spread operator (`...`) to create a new array by copying the first part of the original array, removing the last element, creating a copy of the remaining elements, and then concatenating them with the removed element at the end. 3. **Concat**: This approach uses the `concat` method to concatenate three arrays: one containing the removed element, one containing the remaining elements before the last one, and one containing the remaining elements after the last one. **Options compared:** These three approaches are compared in terms of their execution time on different devices. **Pros and Cons:** * **Splice (non-mutating)**: * Pros: * This approach is relatively efficient because it modifies the original array in place. * It avoids creating intermediate arrays, which can reduce memory usage. * Cons: * The `splice` method has a higher overhead compared to other methods like spread and concat. * The operation involves removing an element from the array, which can be costly. * **Spread**: * Pros: * This approach creates new arrays in place of modifying the original one. * It avoids using the `splice` method or the `concat` method with a large number of elements. * Cons: * Creating new arrays can be memory-intensive, especially for large arrays. * The operation involves copying a significant portion of the array, which can slow down performance. * **Concat**: * Pros: * This approach is simple and easy to understand. * It avoids using intermediate arrays or the `splice` method. * Cons: * Creating multiple temporary arrays can lead to high memory usage. * The operation involves concatenating a large number of elements, which can slow down performance. **Libraries:** None of these test cases use any external libraries. **Special JS features/syntax:** There are no special JavaScript features or syntax used in these benchmark tests. They only utilize built-in methods and operators like `splice`, `concat`, and the spread operator (`...`). **Alternatives:** Other alternatives for reordering elements in an array include: * Using a custom loop to manually iterate over the array and insert or remove elements. * Utilizing data structures like queues or stacks, which are designed for inserting and removing elements at specific positions. * Leveraging modern JavaScript features like `Array.prototype.slice()` with a custom function to reorder elements. However, these alternatives may not be as efficient as the approaches tested in this benchmark.
Related benchmarks:
array test
array test
last array element
slice vs destruction
JS Array Slice vs Array Spread
Comments
Confirm delete:
Do you really want to delete benchmark?