Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reverse vs toReversed
(version: 2)
Compare Array.reverse() to Array.toReversed() performance wise.
Comparing performance of:
Array.reverse() vs Array.toReversed()
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Tests:
Array.reverse()
array.reverse();
Array.toReversed()
array.toReversed();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.reverse()
Array.toReversed()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.reverse()
28036914.0 Ops/sec
Array.toReversed()
8826021.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test and explain what's being tested. **Benchmark Test:** Measuring the performance of `Array.reverse()` vs `Array.toReversed()` The provided JSON represents a benchmark test that compares the performance of two different methods to reverse an array in JavaScript: `Array.reverse()` and `Array.toReversed()`. **Options Compared:** * `Array.reverse()`: This method reverses the order of elements in the array by swapping adjacent pairs of elements. * `Array.toReversed()`: This method returns a new reversed copy of the array, without modifying the original array. **Pros and Cons:** * **`Array.reverse()`**: Pros: + It's a built-in method in JavaScript, so it's likely to be implemented efficiently by the engine. + It modifies the original array, which might be desirable in some cases. * Cons: + It reverses the entire array in place, which can lead to performance issues if the array is large or frequently updated. + It doesn't create a new reversed copy of the array, so it affects the original data. * **`Array.toReversed()`**: Pros: + Creates a new reversed copy of the array, preserving the original data. + Does not modify the original array, which can be beneficial in certain scenarios. * Cons: + It's a custom method implemented by the browser engine or test runner, so its performance might vary across different engines. + It creates a new array object, which consumes more memory. **Other Considerations:** * The benchmark only measures the performance of these two specific methods. Other approaches to reverse an array, such as using `slice()` and `reverse()`, are not compared in this test. * The benchmark assumes that the input arrays are large enough to demonstrate significant performance differences between the two methods. **Library Usage:** In the provided code snippets, there is no explicit library usage mentioned. However, it's worth noting that `Array.reverse()` and `Array.toReversed()` are built-in methods in JavaScript, so they don't rely on external libraries. **Special JS Features or Syntax:** There are no special JavaScript features or syntax used in this benchmark test. It only employs standard JavaScript methods and data structures. **Alternatives:** If you're interested in exploring alternative approaches to reverse an array, here are a few options: * Using `slice()` and `reverse()`: This approach creates a new reversed copy of the array by slicing it from the beginning and reversing the slice. ```javascript const arr = [1, 2, 3, 4, 5]; const reversedArr = arr.slice().reverse(); ``` * Using `reduceRight()` with an initial value: This approach uses the `reduceRight()` method to iterate over the array from right to left and accumulate the elements in a new reversed array. ```javascript const arr = [1, 2, 3, 4, 5]; const reversedArr = arr.reduceRight((acc, curr) => [...acc, curr], []); ``` Keep in mind that these alternatives might have different performance characteristics or memory implications compared to the built-in `Array.reverse()` and `Array.toReversed()` methods.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Array clone from index 1 to end: spread operator vs slice
`array.slice(-1)[0]` vs `array[array.length - 1]`
Array.slice(-1) vs Array.At(-1) Teste Pro Eduzeira
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?