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

  • Home
  • SEARCH
  • 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 6719737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:07:42+00:00 2026-05-26T09:07:42+00:00

What is best practice for switching two items place in an Array via JavaScript?

  • 0

What is best practice for switching two items place in an Array via JavaScript?

For example if we have this array ['one', 'two', 'three', 'four', 'five', 'six'] and want to replace two with five to get this final result: ['one', 'five', 'three', 'four', 'two', 'six'].

enter image description here

The ideal solution is short and efficient.

Update:

The var temp trick is working and I know it. My question is: is using JavaScript native Array methods faster or not?

Something like array.splice().concat().blah().blah() seems is faster and use less memory. I didn’t develope a function that using Array methods but I’m sure it’s possible.

  • 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-05-26T09:07:43+00:00Added an answer on May 26, 2026 at 9:07 am

    Just use a variable as temporary storage:

    var temp = data[1];
    data[1] = data[4];
    data[4] = temp;
    

    As this just copies values in the array it avoids methods like slice and splice that move around big parts of the array or creates entirely new arrays, which would be very expensive in comparison, and also scales badly. This is an O(1) operation, while splicing would be an O(n) operation where n is the number of items that would be moved, which would be something like length*2-index1-index2.

    Here is a performance test for different ways of swapping the items: http://jsperf.com/array-item-swap/2

    Even if you use splice in a way so that it would not need to move around a lot of data, it’s still slow. Copying the values is about 50 times faster.

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

Sidebar

Related Questions

This is a general best practice question about creating parent / child relationships with
Best practice for developing .NET programs seems to be this: never reference dlls from
What is the best practice for implementing a database revisioning? Assume i have a
What is the best practice for referencing nested objects? Let's say I have the
Best practice when converting DataColumn values to an array of strings? [Edit] All values
Does anybody know the best practice approach to getting an array of CCMenuItems to
Best practice is to use unique ivs, but what is unique? Is it unique
Short best practice question: If an object A is injected into another object B,
The 'best practice' (as I see it) to atomically create a new file, is
What's the best practice for making sure that certain ajax calls to certain pages

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.