Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:07:41+00:00 2026-06-14T12:07:41+00:00

A simple implementation for reversing an array is twice as fast compared to the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:07:42+00:00Added an answer on June 14, 2026 at 12:07 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html multiline tooltip implementation: this.tooltip = function(tag) { xOffset =
I wrote a simple implementation of the Game of life with java applets. Here's
Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/ Here it is: template<typename
We're doing a simple implementation of Google Analytics on our ASP.NET with jQuery/AJAX web,
This is a simple implementation of a sort algorithm. My question is. The array
How can the following simple implementation of sum be faster? private long sum( int
I'm using a simple implementation of saxParser. Within my endElement method, I'm storing vo
One more question about most elegant and simple implementation of element combinations in F#.
I was writing an answer to this question when noticed that my simple implementation
I am writing a simple Vector implementation as a Python extension module in C

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.