JavaScript What is the most performant way to concatenate a string with a separator

let s = arr[0] || "";
for (let i=1; i < arr.length; i++) s += sep + arr[i];

Add the first element, start looping at the second, adding the separator in front.

As you point out in your question, you could use a condition to only append the separator when needed, removing the need to get rid of the redundant one. The problem is, it might be correct if you are joining two or three values, but having to test in every loop iteration will get costly with big arrays.

Answer:1

For the first element, you could check for the length of the array and take the stringed value or an empty string.

Then loop as Salketer in his answer is doing:

let s = arr.length && arr[0].toString() || '';
for (let i=1; i < arr.length; i++) s += sep + arr[i];
Answer:2

If you want to define it yourself you can iterate till to the length- 1. After the loop you can add the last item only without separator.

Example

let arr = [1,2,3,4,5];
let sep = '-';

function customJoin(array, separator) {

   if(array.length === 0) {
      return '';
   }

   let s = '';
   let length = array.length;

   for (let i = 0; i < length - 1; i++) {
      s += arr[i] + separator;
   }

   return s + arr[length - 1];
}

console.log(customJoin(arr, sep));
Answer:3

Using reduce:

function join(arr, sep){
  if (!arr.length)
    return '';

  return arr.reduce((a,b) => a + sep + b);
}

console.log(join([1,2],'^'));
Answer:4

When editing a row on jqgrid, the cursor automatically enters the left-most editable field. Is there a way to have it default to a particular column, or better yet, to whatever column I click on?

When editing a row on jqgrid, the cursor automatically enters the left-most editable field. Is there a way to have it default to a particular column, or better yet, to whatever column I click on?

  1. have cursor start text field

I noticed the following in a React app: <UserList {...{ userIdsTyping, users }} /> What exactly is {...{ userIdsTyping, users }} doing here? I understand it's passing children to the ...

I noticed the following in a React app: <UserList {...{ userIdsTyping, users }} /> What exactly is {...{ userIdsTyping, users }} doing here? I understand it's passing children to the ...

  1. react ...props syntax
  2. react props spread syntax
  3. react default props syntax

I currently have the following code: <div id="resource1"></div> <div id="resource2"></div> <div id="resource3"></div> <script> var resource1 = 50; var ...

I currently have the following code: <div id="resource1"></div> <div id="resource2"></div> <div id="resource3"></div> <script> var resource1 = 50; var ...

I want to output "You are logged in as [Username]" using i18next for the text and a Jade variable as the actual username, but I can't seem to get them working together. As far as i can tell, i18n ...

I want to output "You are logged in as [Username]" using i18next for the text and a Jade variable as the actual username, but I can't seem to get them working together. As far as i can tell, i18n ...