T
T
thsiganenko2015-09-24 20:21:25
JavaScript
thsiganenko, 2015-09-24 20:21:25

Is JavaScript array reversal properly tested?

Good day, dear community members!
In the book Expressive JavaScript by Marain Haverbeck, it is suggested as an exercise to write an array reversal function, that is, you need to take an array and reverse it so that the elements that were at the beginning are at the end. There is a standard .reverse() method for arrays that performs this function.
This problem has already been mentioned among the questions, and a number of interesting solutions have been proposed.
For training, I solved the problem in several ways and decided to check how long each option takes.
I have implemented 4 functions.
First option with a While loop:

function reversInPlaceWhile(arr) {
    var len = arr.length - 1;
    var i = 0, temp;
    while (i < len - i) {
        temp = arr[i];
        arr[i] = arr[len - i];
        arr[len - i] = temp;
        i++;
    }
    return arr;
}

Second option with a For loop
function reversInPlaceFor(arr) {
    var len = arr.length - 1;
    var temp;
    for (var i = 0, iter = Math.floor(len / 2); i < iter; i++) {
        temp = arr[i];
        arr[i] = arr[len - i];
        arr[len - i] = temp;
    }
    return arr;
}

Option three, using the standard method:
function reversInPlaceStandart(arr) {
    arr.reverse();
    return arr;
}

Option four, using the splice() method
(this option was suggested by one of the community members)
function reversInPlaceSplice(arr) {
    var len = arr.length - 1;
    for (var i = 0; i < len; i++) {
        arr.splice(i, 0, arr.pop());
    }
    return arr;
}

For testing purposes, I created a function that generates a simple array of the desired size:
function newArrayLine(count) {
    var arr = [];
    for (var i = 0; i < count; i++) {
        arr[i] = i;
    }
    return arr;
}

And finally, for testing purposes, I used the function:
function test(arr, iter, callback) {
    var start = Date.now();
    for (var i = 0; i < iter; i++) {
        callback(arr);
    }
    var end = Date.now();
    return end - start;
}

where arr is the array that will be turned over
iter is the number of times to be turned over
callback is the function with which we will turn the array
The result I expected to get in milliseconds.
Running this function on an array of 10,000 elements, 100 times, in the Mozilla Firefox browser, I got the following results:
for a function with a while loop: 1 ms ;
for a function with a for loop : 1 ms ; for a function with a standard reverse()
method : 12 ms ; for a function with a splice() method: 6562 ms ; Running this function on an array of 100,000 elements, 100 times, in the Chome browser, I got the following results:

for a function with a while loop: 7 ms ;
for a function with a for loop : 6 ms ;
for a function with a standard reverse() method : 25 ms ;
for a function with a splice() method: 485163 ms ;
I ran the functions several times, the results are about the same.
Surprisingly, the built-in method is slower than those implemented with loops.
And now the main question itself: was the testing carried out correctly, and can the data be considered representative?
And one more question, are there any errors or shortcomings in my code that I should pay attention to?
Thanks in advance to those who decided to take the time to my question!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-09-24
@alexey-m-ukolov

It looks like the truth.

T
thsiganenko, 2015-09-25
@thsiganenko

copal And if thus. Although something strongly confuses me with such an approach, I will ask anyway.
We create a reversArray object and it has three methods with different implementations of array reversal.
Next, we call it with code:

function testOne() {
    var start = Date.now();
    reversArray.reversInPlaceWhile(newArrayLine(100000));
    var end = Date.now();
    return end - start;
}

And for the native function, we use the following code:
function testTwo() {
    var start = Date.now();
    newArrayLine(100000).reverse();
    var end = Date.now();
    return end - start;
}

It feels like I started to confuse "warm" with "soft", I just can't figure out where the mistake is.
It works, the result is not much different in terms of speed, but the native method with this approach gives more execution time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question