How exactly does Javascript's array.reverse()
work? Does it go through and swap every element of the array? If so, does it take O(n) to swap an array of size n?
I guess the reason I am asking is because I was wondering if array.reverse()
was the same as:
for(var i = 0; i < a.length / 2; i++) {
var holder = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = holder;
}
NOTE: Sorry if the Javascript code I posted is incorrect, it's pretty late right now.
EDIT: Fixed a.length
to a.length / 2
.
JavaScript Array reverse()The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.
reverse() if you want in-place, or array. slice(). reverse() if you want a copy. Standard libs are already likely to be fastest, are undoubtedly cleanest to understand, and their speed increases for free as they're optimized over time.
Array.prototype.reverse() The reverse() method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first.
For the full details of how it works, read the relevant section of the spec. Here's the algorithm:
Let O be the result of calling ToObject passing the this value as the argument.
- Let lenVal be the result of calling the [[Get]] internal method of O with argument "length".
- Let len be ToUint32(lenVal).
- Let middle be floor(len/2).
- Letlower be 0.
Repeat, while lower ≠ middle
- Let upper be len−lower −1.
- Let upperP be ToString(upper).
- Let lowerP be ToString(lower).
- Let lowerValue be the result of calling the [[Get]] internal method of O with argument lowerP.
- Let upperValue be the result of calling the [[Get]] internal method of O with argument upperP .
- Let lowerExists be the result of calling the [[HasProperty]] internal method of O with argument lowerP.
- Let upperExists be the result of calling the [[HasProperty]] internal method of O with argument upperP.
If lowerExists is true and upperExists is true, then
Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .
- Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
- Else if lowerExists is false and upperExists is true, then
- Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .
- Call the [[Delete]] internal method of O, with arguments upperP and true.
- Else if lowerExists is true and upperExists is false, then
- Call the [[Delete]] internal method of O, with arguments lowerP and true .
- Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
- Else, both lowerExists and upperExists are false
- No action is required.
- Increase lower by 1.
- Return O .
The actual algorithm is almost similar to what you specified. Just change your for
loop to iterate only upto a.length/2
and it would be similar to what Array.reverse
would do. I am skipping the inner details here for the sake of simplicity. So it would be
for(var i = 0; i < a.length/2; i++) {
var holder = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = holder;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With