Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reverse vs toReversed of 10000 elements
(version: 1)
Compare Array.reverse() to Array.toReversed() performance wise on an array of size 10000
Comparing performance of:
Array.reverse() vs Array.toReversed()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
array = Array.from({length: 10000}).map((_, i) => { number: i })
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.reverse()
154328.6 Ops/sec
Array.toReversed()
73297.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON compares the performance of two methods used to reverse an array in JavaScript: `Array.reverse()` and `Array.toReversed()` on an array of 10,000 elements. ### Options Compared 1. **Array.reverse()** - Definition: This method reverses the elements of the array in place and returns the same array reference. - Pros: - In-place operation: It modifies the original array, which can save memory because it does not require the allocation of additional space for a new array. - Generally fast for smaller arrays or scenarios where modification is acceptable. - Cons: - Side effects: Since it modifies the original array, it can lead to unintended consequences if the array is needed in its original order after the operation. - Not functional: It does not adhere to functional programming principles, which may be a concern in certain code architectures. 2. **Array.toReversed()** - Definition: This method creates a new array with the elements in reverse order, leaving the original array unchanged. - Pros: - Non-destructive: It preserves the original array, making it safer to use in contexts where you want to keep the original order intact. - Matches functional programming principles: It’s purely functional since it returns a new array. - Cons: - Memory overhead: It requires additional memory allocation for the new reversed array, which may impact performance for very large arrays. - Potentially slower than in-place operations, especially as the size of the array increases. ### Library and Syntax In this benchmark, both methods are built-in JavaScript Array methods, and no external libraries are involved. They are part of the ECMAScript specification and are widely supported across different JavaScript environments. The benchmark also utilizes standard JavaScript syntax for creating the array using `Array.from()` and the mapping function, which is an effective way to generate an array of a specified length filled with objects or numbers. ### Performance Results The benchmark results indicate that `Array.toReversed()` was slightly faster than `Array.reverse()`, with execution speeds of approximately 114,318 vs. 113,878 executions per second, respectively. ### Other Considerations and Alternatives - If memory usage is a critical factor, one might prefer using `Array.reverse()`, especially when working with large datasets where keeping the original array is not a concern. - An alternative approach could involve using libraries such as Lodash, which provides utility functions that can work with arrays in a more functional style. However, for merely reversing arrays, sticking with native methods like these is generally recommended for performance and simplicity. Overall, developers need to assess the specific requirements of their application, such as performance, memory usage, and whether the original data needs to be retained, when choosing between these two methods.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Array deconstruction vs array push
Array.from() vs new Array() vs [..Array()]
Array.from() vs [...new Array()]
Array.from() vs new Array() + destruct
array[array.length - 1] vs array.at(-1) 3
array[array.length - 1] vs array.at(-1) 4
toSpliced vs Spread
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?