Codewars Challenge Kata
Do you need to iterate?

There is a lot of room for improvement..

Use one loop if possible.

You have many loops where you iterate the same number of times. Rather than doing each loop in turn do as much as possible in one loop.

Eg. 

while (!(9 * slots &gt;= n)) { 
  slots += 1;
}
while (slotsArray.length != slots) {
  slotsArray.push(9);
}


Can be done in one

// Note new solution does not require extra item
while (!(9 * slots &gt;= n)) { 
    slots += 1;
    slotsArray.push(9);
}


Simple conditions

Make the condition statement as simple as possible. You had !(9*slots &gt;= n) the additional not clause is extra work (if not optimised) 9*slots &lt; n is the same 

Use variables

Some javascript engines (V8) are not as quick handling literal numbers. eg x *= 10 is slower than x *= ten but you must take into account the declaration and assignment of the variable. If you use the number once or twice there is no advantage, but if you do it many time there is.

Math rather than Loops

Alway check to see if there is a way to directly calculate the result rather than use iteration.

You do not actually need any loops

Use memory

Memory is cheap and fast, CPU cycles are not. So rather than iterate have a predefined object that can be used to create a result

You have

  while (finalOutput[1].length != n) {
    finalOutput[1] += '1';
  }


but the following is quicker (note it should be defined outside the function but in this case you may only have the function's scope)

  // long enough for all possible results?
  var ones = "111111111111111111111111111111111111111111111111111111111";
  finalOutput[1] = ones.substr(0,n);


There are many other optimisations that I have not included as the resulting optimised code is very simple and most of what you had is not needed.

Testing

To ensure I got it correct I wrote a test that compares your function to the new one. Making sure the results are the same, then running each version 100,000 times to get the performance.

There results are

// calls to function oneTwoThree Ori is the original and New is the optimised version
// time in ms (1/1000);

-- Test value : (19) -------------------
Ori : 191.53ms Mean : 0.00192
New : 22.38ms Mean : 0.00022
-- Test value : (1) -------------------
Ori : 8.78ms Mean : 0.00009
New : 6.61ms Mean : 0.00007
-- Test value : (7) -------------------
Ori : 62.03ms Mean : 0.00062
New : 6.02ms Mean : 0.00006
-- Test value : (36) -------------------
Ori : 208.12ms Mean : 0.00208
New : 18.57ms Mean : 0.00019
-- Test value : (0) -------------------
Ori : 8.47ms Mean : 0.00008
New : 4.00ms Mean : 0.00004


For the results of n &lt; 10 the improvement is not that great but near 25%-50% is worth the effort. The improvement for n >= 10 are well worth the effort.

The new code

No loops needed. Warning longStr1s may need to be larger as I have not tested all possible inputs.

function oneTwoThree_Q(n) {
    var nine = 9; // variable = 9 is quicker than litteral value
    var slots;
    var longStr1s = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
    var longStr9s = "999999999999999999999999999999999999999999999999999999999";
    if (n &lt; 10) {
        return ['' + n, n === 0 ? '' + n : longStr1s.substr(0,n)];
    }
    slots = Math.ceil(n / nine);
    nine += n - slots * 9;
    return [longStr9s.substr(0,slots-1) + nine, longStr1s.substr(0,n)];
}