Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reverse vs toReversed [1000 elements of strings]
(version: 0)
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 = []; for (let i = 0; i < 1000; i++) { array.push(btoa(Math.random().toString()).substring(10,15)); }
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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.reverse()
549972.5 Ops/sec
Array.toReversed()
801455.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of two JavaScript methods: `Array.reverse()` and `Array.toReversed()`. The test creates an array with 1000 elements, where each element is a string created using `btoa` (binary-to-text encoding) with a random string. This helps distribute the workload evenly across the array. **Options Compared** The two options being compared are: 1. **`Array.reverse()`**: Reverses the order of the elements in the array. 2. **`Array.toReversed()`**: Not a standard JavaScript method, but rather a custom implementation using `Array.prototype.map()`, which returns a new array with the same elements in reversed order. **Pros and Cons** * **`Array.reverse()`**: + Pros: It's a widely supported, built-in method that can be easily implemented. + Cons: It modifies the original array, can be slower for large datasets due to the overhead of reversing the entire array. * **`Array.toReversed()`**: + Pros: It creates a new reversed array without modifying the original one, which might be beneficial for preserving data integrity. + Cons: It's not a standard method and may not be supported by all browsers or environments. **Other Considerations** When testing performance, it's essential to consider factors like: * Cache behavior: How does each method affect cache hits/misses? * Loop unrolling: Can the methods be optimized for loop unrolling or other optimization techniques? **Library Usage** In this benchmark, `btoa` is used to create random strings. `btoa` is a binary-to-text encoding function that converts binary data into a text representation. Its purpose is to encode binary data (like images) as text. **Special JS Feature or Syntax** There's no special JavaScript feature or syntax being tested in this benchmark. The focus is on the performance comparison between two methods. Now, regarding alternatives: * **Other approaches for reversing an array**: Instead of using `Array.reverse()` or `Array.toReversed()`, you could use other algorithms like: + Iterative approach with a loop that swaps elements from both ends. + Using a queue data structure to store the elements in reverse order. * **Alternative methods for creating reversed arrays**: + Use `Array.prototype.map()` and `String.prototype.split()`/`String.prototype.slice()` to create a new reversed array. These alternatives can be explored to see if they offer better performance or more efficient ways to achieve the same result.
Related benchmarks:
arr unshift vs push + reverse (large array)
JavaScript reverse vs toReversed
JavaScript reverse with copy vs toReversed
Reverse vs ToReversed
Comments
Confirm delete:
Do you really want to delete benchmark?