5
\$\begingroup\$

I was asked to print numbers from 100 to 1 in a for loop with the index starting from 1. I came up with this solution:

var a = [];
for(var i = 1; i <= 100; i++) {
 a.push(i);
}
console.log(a.reverse());

I was thinking of a better way of doing this after the interview and came up with this

for(var i = 1; i <= 100; i++) {
 console.log((100-i)+1);
}

Now what blows my mind is, jsperf.com's test indicates the former is faster. How is this even possible? Creating an array, pushing something into it and reversing it before printing definitely seems slower than simple arithmetic. Could anyone help me understand this?

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Feb 10, 2018 at 3:39
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I honestly think this should probably be on stackoverflow instead of codereview. Nevertheless, really interesting question. \$\endgroup\$ Commented Feb 10, 2018 at 6:43
  • 2
    \$\begingroup\$ Figure out the number of calls to console.log(). Try timing building the output as a single string, and logging that. \$\endgroup\$ Commented Feb 10, 2018 at 9:07

3 Answers 3

3
\$\begingroup\$

It turns out accessing console take's more time than pushing a value in an array.

Console.log is indeed implemented as a synchronous function (next print starts only after last print finishes).

Meaning, there is no buffering system. The program goes to blocking state until console.log() finish printing, everytime.

Performance difference in above two cases is due to the difference in the number of console.log() call.

answered Feb 10, 2018 at 4:30
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The test results are meaningless, the error on 4 samples ranges 2 orders of magnitude. That is statistically called noise. You need to test 1000+ samples, 1000+ times, include the error bars. Then you can make a deduction about performance, use that to make a prediction, and only when you confirm your prediction via experiment are you able to make a meaningful statement about performance. \$\endgroup\$ Commented Feb 10, 2018 at 5:51
  • \$\begingroup\$ Thank you, sir. Though I am not predicting anything, It was only for demonstration. \$\endgroup\$ Commented Feb 10, 2018 at 10:07
1
\$\begingroup\$

You can also push the reversed order of values in the array so you can avoid reverse() and can call console.log() outside the loop.

var a = [];
for(var i = 1; i <= 100; i++) {
 a.push(101-i);
}
console.log(a)
answered Feb 13, 2018 at 9:53
\$\endgroup\$
0
\$\begingroup\$

Both variants presented look somewhat indirect. What about

for (var i = 100 ; 1 <= i ; --i)
 console.log(i);

(I grant that print numbers from 100 to 1 in a for loop with the index starting from 1 reads use an array - not sure about starting from 1.)

for (var index = 1 ; 1 <= (99+index) ; --index)
 console.log(99+index);
answered Feb 10, 2018 at 9:28
\$\endgroup\$
1
  • \$\begingroup\$ Personally preferring for (var i = 101 ; 0 < --i ; ). \$\endgroup\$ Commented Feb 10, 2018 at 9:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.