Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reverse array
(version: 0)
Comparing performance of:
Spread and reverse vs Slice and reverse vs For loop
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function reverse (arr) { var newArray = []; var index = arr.length - 1; while (index >= 0) { newArray.push(arr[index]); index--; } return newArray; } var arr = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultricies sagittis odio, et mollis erat mattis ut. In mattis massa nunc, sit amet hendrerit ligula accumsan eu. Pellentesque rutrum aliquet laoreet. Nunc rutrum dignissim pretium. Maecenas non nisl a ligula varius eleifend eget non nulla. Donec dictum sit amet lorem consequat elementum. Mauris scelerisque vestibulum interdum. Ut sit amet lorem posuere, interdum neque et, fermentum turpis. Morbi tincidunt, ante vel egestas efficitur, odio urna tincidunt mi, sit amet mattis lorem ante at sem. Etiam porta sodales sagittis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ultricies sem a nunc convallis, ut molestie lacus elementum. Donec pellentesque nulla tellus, id porta dolor molestie non. Integer lacus diam, pharetra nec lacus a, sollicitudin dapibus nibh. Duis varius metus ante, eu ultrices purus cursus in. Sed molestie, sem sit amet rutrum interdum, elit dui aliquam arcu, a fringilla justo nibh in est. Nulla ac elit eget massa pellentesque congue. Phasellus quis mauris non diam luctus posuere. Nullam porta diam auctor volutpat bibendum. Duis lorem leo, mollis non convallis lacinia, faucibus ac orci. Sed eu velit et quam facilisis condimentum. Pellentesque egestas, lorem ut laoreet maximus, metus leo aliquam ligula, a luctus lectus ligula eget purus.'.split('');
Tests:
Spread and reverse
var result = [...arr].reverse();
Slice and reverse
var result = arr.slice(0).reverse();
For loop
var result = reverse(arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread and reverse
Slice and reverse
For loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Browser/OS:
Firefox 139 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread and reverse
113888.1 Ops/sec
Slice and reverse
345515.8 Ops/sec
For loop
237712.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an explanation of the benchmark, options being compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests three different approaches for reversing an array: 1. **Spread and reverse**: `var result = [...arr].reverse();` 2. **Slice and reverse**: `var result = arr.slice(0).reverse();` 3. **For loop**: `var result = reverse(arr);` (a custom function defined in the Script Preparation Code) The benchmark compares the performance of these three approaches on a large array of strings. **Options being compared** * Spread operator (`...`) vs. slicing (`slice()`): Two different methods to create a copy of the original array. * Reverse operation: Both approaches reverse the array, but with different overheads and efficiency. **Pros and cons of each approach** 1. **Spread and reverse**: * Pros: Efficient use of spread operator, creates a new array without modifying the original one. * Cons: May incur additional overhead due to the spread operator's complexity. 2. **Slice and reverse**: * Pros: Faster than spread operator due to caching and optimization in modern browsers. * Cons: Creates a reference to the original array, which may lead to unexpected behavior if modified later. 3. **For loop**: * Pros: Directly reversing the array using a custom function, without creating additional intermediate arrays. * Cons: May be slower due to manual iteration and looping overhead. **Other considerations** * **Browser caching**: The benchmark result is likely influenced by browser caching mechanisms, which may affect the performance of repeated runs. * **Array size**: The large input array used in this benchmark might not accurately represent smaller use cases, where more efficient algorithms like `slice()` or spread operator might dominate. * **Modernization**: As JavaScript features and APIs evolve, benchmark results might change. For example, the `map()` method may become a viable alternative for reversing arrays. **Library usage** There is no explicit library usage in this benchmark, but some libraries (e.g., jQuery) might offer similar array manipulation functions that could influence benchmark results. **Special JavaScript feature or syntax** The spread operator (`...`) was introduced in ECMAScript 2015 and provides a concise way to create new arrays. It's supported by modern browsers but may incur additional overhead compared to older browsers or environments that don't support this feature. **Alternatives** For reversing arrays, other approaches could include: * **Using `Array.prototype.reverse()`**: A built-in method for reversing an array in place. * **Lodash library functions**: Lodash provides various utility functions for manipulating arrays, including `_.reverse()`. * **Other array manipulation libraries or frameworks**: Some libraries (e.g., React) offer custom solutions for array manipulation. Keep in mind that the best approach depends on specific use cases and performance requirements.
Related benchmarks:
for loop reverce
arr unshift vs push + reverse ( array 100)
arr unshift vs push + reverse (mid array)
arr unshift vs push + reverse (size 50 array)
arr unshift vs push + reverse (recreating size 50 array)
Comments
Confirm delete:
Do you really want to delete benchmark?