Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
MMP Reverse Array
(version: 0)
Comparing performance of:
slice().reverse() vs toReversed() vs swap destructure
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = [...Array(100000).keys()]; var r = [];
Tests:
slice().reverse()
r = a.slice().reverse();
toReversed()
r = a.toReversed();
swap destructure
let i = 0, j = a.length - 1; while (i < j) [r[i++], r[j--]] = [a[j], a[i]];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
slice().reverse()
toReversed()
swap destructure
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
slice().reverse()
698.9 Ops/sec
toReversed()
437.4 Ops/sec
swap destructure
247.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested in this benchmark. **Overview** This benchmark tests different ways to reverse an array of numbers using JavaScript. The benchmark is designed to measure the performance of these reversal techniques across various browsers and devices. **Benchmark Definition JSON** The provided JSON defines a single benchmark: * `Name`: "MMP Reverse Array" * `Description`: null (no description provided) * `Script Preparation Code`: This code creates an array of 100,000 numbers using the `Array` constructor and assigns it to variable `a`. * `Html Preparation Code`: null (no HTML preparation code is required) **Individual Test Cases** There are three test cases: 1. **`slice().reverse()`**: This test case uses the built-in `slice()` method followed by `reverse()`. The idea behind this approach is that `slice()` returns a new array with a reference to the original array, and then `reverse()` reverses the elements of the new array. 2. **`toReversed()`**: This test case uses an undefined function `toReversed()`, which is not a standard JavaScript method. It's likely that this function is specific to a library or framework being tested (more on this later). 3. **Swap destructure**: This test case uses a custom loop-based approach, where two pointers are swapped in place using destructuring assignment. **Pros and Cons** Here's a brief analysis of each approach: * **`slice().reverse()`**: Pros: concise and easy to implement. Cons: creates a new array, which might lead to performance issues if the original array is large. * **`toReversed()`**: Pros: none known (likely specific to a library or framework). Cons: undefined function, potentially slower due to unknown implementation. * **Swap destructure**: Pros: no additional data structures are created. Cons: more complex and harder to understand. **Other Considerations** * The benchmark uses a large array of 100,000 numbers, which is likely chosen for its performance characteristics rather than practicality. * The test cases do not account for edge cases like empty arrays or single-element arrays. **Library/Frame Work Mentioned: `toReversed()`** The function `toReversed()` is likely specific to a library or framework that provides an optimized reversal method. Without further information, it's difficult to determine the specifics of this implementation. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Alternatives** Other ways to reverse an array include: * Using `Array.prototype.reverse()` directly * Using a custom algorithm like the "loop-destruction" approach (similar to swap destructure) * Using a third-party library like Lodash, which provides a `reverse` method Keep in mind that these alternatives might not be as performance-optimized as the specific methods being tested here.
Related benchmarks:
Array Assignment
array push
array last element big data
test1235161321
shallow copy of 6M elements array
Comments
Confirm delete:
Do you really want to delete benchmark?