Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reverse with copy vs toReversed
(version: 0)
Compare Array.reverse() with copy to Array.toReversed() performance wise.
Comparing performance of:
Array.reverse() vs Array.toReversed()
Created:
one year ago
by:
Guest
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 year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.reverse()
18954262.0 Ops/sec
Array.toReversed()
33542014.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to reverse an array: `Array.reverse()` and `array.toReversed()`. The test case creates an array of 10 elements, reverses it using both methods, and measures their performance in terms of execution time per second (`ExecutionsPerSecond`). **Options Compared** Two options are being compared: 1. **`Array.reverse()`**: This method reverses the array in-place, modifying the original array. 2. **`array.toReversed()`**: This method creates a new reversed copy of the array. **Pros and Cons of Each Approach** * `Array.reverse()`: Pros: + More efficient for large arrays, as it modifies the original array and doesn't create a new one. + Can be faster in terms of execution time. Cons: + Modifies the original array, which might not be desirable if the original array needs to remain unchanged. + Can lead to unexpected behavior if other methods rely on the original order of elements. * `array.toReversed()`: Pros: + Creates a new reversed copy of the array, leaving the original array unchanged. Cons: + More memory-intensive, as it creates a new array with the same number of elements but in reverse order. + Can be slower for large arrays due to the overhead of creating a new array. **Library and Its Purpose** In this benchmark, `Array.toReversed()` is using a library or implementation that provides an `toReversed()` method on arrays. This method likely creates a new reversed copy of the array using some internal algorithm, such as reversing the elements in place or using a temporary array. The specific implementation details are not provided in this benchmark. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being used in this benchmark that would require explanation beyond the standard language features. However, if we were to extend this benchmark to include other approaches, such as: * Using `slice()` and `reverse()`: `array.slice().reverse()` * Using `forEach()` and modifying indices: `array.forEach((element, index) => { array[index] = array[9 - index]; });` * Using a custom implementation with iteration or recursion Please let me know if you would like explanations for those alternatives.
Related benchmarks:
unshift vs reverse push reverse
JavaScript reverse vs toReversed
JavaScript reverse vs toReversed [1000 elements of strings]
Reverse vs ToReversed
Comments
Confirm delete:
Do you really want to delete benchmark?