2

A simple implementation for reversing an array is twice as fast compared to the built in function in Javascript, when tested in Chrome. What's V8 doing? Here is the test:

var newArr = [];
var newArrDefault = [];
for(var i = 0; i < 10000000; i++){
    newArr[i] = i;
    newArrDefault[i] = i;
}


var startDefault = new Date();
newArrDefault.reverse();
console.log("Built in method took " + (new Date().getTime() - startDefault.getTime()));


var start = new Date();
for(var i = 0; i < newArr.length / 2; i++){
    var tmp = newArr[i];
    newArr[i] = newArr[newArr.length-i-1];
    newArr[newArr.length-i-1] = tmp;
}
console.log("Custom method took " + (new Date().getTime() - start.getTime()));

Results on Version 20.0.1132.47 Ubuntu 12.04 (144678):

Built in method took 149
Custom method took 71
6
  • 2
    Your result is confirmed in jsPerf jsperf.com/arrayreversecomparison in Chrome. Custom function is faster than built in function. Here's the implementation spec for .reverse() stackoverflow.com/a/7912713/816620
    – jfriend00
    Nov 19, 2012 at 1:55
  • 1
    @jfriend00 Actually the results of that jsperf case shows that: the built in function is faster than the custom one.
    – Dan D.
    Nov 19, 2012 at 2:11
  • 1
    One difference is that conforming implementations test whether members exist at each index. The OP doesn't, so it will create members where none previously existed (and therefore isn't consistent with ECMA-262). Each index (upper and lower) should be tested with hasOwnProperty first.
    – RobG
    Nov 19, 2012 at 2:18
  • The implementation per ECMA spec does lots of special handling on undefined entries. It doesn't blindly just swap. This requires more tests on each value.
    – jfriend00
    Nov 19, 2012 at 2:43
  • @DanD. - The jsperf seems inconsistent. In 5 runs in Chrome, I've seen the native implementation both faster and slower with a meaningful difference (like 50%). In Firefox 16, the custom function seems to be faster for me too. In any case, I think the answer is that the ECMAScript version is doing more special handling for missing array entries.
    – jfriend00
    Nov 19, 2012 at 2:46

1 Answer 1

2

For the fun of it, I implemented the specification like so:

var upper, upperExists, lowerExists, lowerValue, upperValue;

for(var lower = 0, len = newArr.length >>> 0, middle = Math.floor(len / 2); lower != middle; ++lower) {
    upper = len - lower - 1;
    lowerValue = newArr[lower];
    upperValue = newArr[upper];
    lowerExists = newArr.hasOwnProperty(lower);
    upperExists = newArr.hasOwnProperty(upper);

    if (lowerExists && upperExists) {
        newArr[lower] = upperValue;
        newArr[upper] = lowerValue;
    } else if (upperExists) {
        newArr[lower] = upperValue;
        delete newArr[upper];
    } else if (lowerExists) {
        newArr[upper] = lowerValue;
        delete newArr[lower];
    }
}

The jsperf can be found here.

It includes a whole bunch of code to deal with missing entries, which is why it's so much slower than both the native and your code (some optimizations may be possible, but it won't affect the performance enough). The performance difference between your code and the native implementation wasn't very conclusive though.

Under most circumstances arrays are a contiguous block of values with no gaps in between, so you should be safe with that kind of code; as long as you know the difference :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.