I was experimenting with javascript for a project i was working on. I tried to log 0 to 1 Million in console because, why not?
I ran the below cod...
For further actions, you may consider blocking this person and/or reporting abuse
Quoted from Steve Baker
source: quora.com/Do-print-statements-in-t...
this one is more optimized, will give you 10-20% faster response.
HI @rc2168st
tried your code
Output
Even the loop completed in 109 ms it took another 2800 ms for the join statement to get completed
& i don't think it took 3 seconds. On my machine this approach was faster then yours.
Set the timer for “join” separately & for console.log separately
Do both approach test in a single script not separate.
yes this code is more bad!
I know this article is about moving the console.log out of the loop.
But ... 😎
~200ms
Aaaand we are ending up with a whopping 6.9MB of string 🤣
and now you have a memory problem. when working with large data in the real world it's better to work in chunks. so instead of logging everything at once you would build up a buffer/queue then empty the buffer/dequeue at an arbitrary interval usually in a background task.
The problem with big data is it's always there. It doesn't really matter how you restructure the problem it's going to cause headache somewhere.
storing in an array and "printing" every xxxxx numbers seems to work even faster indeed
comparing "string build", "array" and "array and flush every xxx"
jsben.ch/rBvkT
Interesting to see such a time difference.
Was this a purely theoretical experiment? Generally, I would only leave
console.log
in the code for debugging purposes, for which it doesn't matter that much if it's slower. Once everything is working, there usually isn't any reason to log to the console.Agree, we should only use console.log only for development/debugging purposes. Even if its a backend or frontend application you should not use it. Instead we can use a logger like raven to catch errrors.
But this is a fun experiment. The first option took time time because an I/O operation was involved in each iteration.
1.Read the value from variable i
2.Print it
Yes, it was an experimental run, I was testing the time taken to run loops and found this
Hi, nice article. I tried it and I seems to have better results with this code, which is pretty close to @rc2168st answer :
output[I] = i
has always been more improved thanoutput.push(i)
++I
vsI++
especially in the case of very large loops like hereBut in reality it seems to depend on browser :
I made a simple benchmark jsbench.me/38leqsrqbt/2
And my solution seems to be faster in Firefox and Safari, but not in Chrome.
Use Output = new Array(1000000) to speed it up a bit more.
In most languages, signaling something to user by using text or visual interface, is quite heavy operation, because (as mentioned in previous comment) theres a lot of stuff which just have to happen.
In first case, calling
console.log
is the heavy part, but in second, its printing the string, because its so big.Try to measure only
console.log
performance, and log string with different length, You will see the differences aswell.Also, have a look at performance API, its more precise than
console
.developer.mozilla.org/en-US/docs/W...