How often do I need to use a while loop, or Array.forEach in a project? All the time of course. I could write a post about what way of looping is the fastest, or what way is the most concise. This is yet another one of my lodash posts, so I will be writing about _.times in lodash naturally, but I will also touch base on some vanilla js alternatives as well.
This is a lodash post on the _.times method that can be used to call a given method a number of times, as well as plain old vanilla js alternatives that bring forth a similar effect. The lodash _.times method is a nice concise solution, but if you care about speed the most first and for most it might be best to stick with native looping.
In this section I will be covering some basic examples of _.times along with some quick vanilla js alternatives. Keep in mid that more concise solutions are not always better solutions all around. I have not taken the time to do synthetic testing with performance in this post, juts throwing together examples.
So here is a quick brainless example of of _.times where I am just calling a method four times. To do this I just call the method and pass the number of times that a function should be called followed by the function.
A while loop example that does the same would look like this.
Some times just using a good old while loop is just whats called for, no need for lodash, and if I just replace let with var, and use old function literals in place of arrow functions this will work on a wide range of browsers as well.
The native array.from method can also be used to call a method a bunch of times as well.
So many solutions like this are a little longer, and maybe Array.from does not have the best browser support when it comes to supporting older non every green browsers. However if most of your site traffic is up to date with there software, using solutions like this can lead to no longer needing lodash as part of a stack.
So there is a bit more that can be done compared to just a simple while loop, such as building a results array by using return in the body of the function passed to _.times. The return keyword can be used in the body of a function to return a value for each element in an array that is returned when using _.times. In this section I will be going over using _.times to do this, as well as vanilla js alternatives for doing so as well.
The times method can be used to build an array, in fact one is always returned when using it, its just by default the values for each element will be undefined if nothing is returned.
This can be useful for a lot of situations, but it is also unnecessary if it is a situation in which I just need to call a method a few times, and the method is not used to build elements in an array.
It’s not like just using a while loop takes that much more effort as well though. So it might not be as concise, by just sticking to while loops I can work just fine without lodash. Also when it comes to working with larger arrays a while loop solution may prove to be faster as well.
So here is the times method I put together
And here is a use case example.
So the times might be a nice quick way to create an array of a fixed length, of call a method a few times. However it might not be the best solution in many cases. There is the issue of speed, and also I can not say that this is one of the best methods that lodash has to offer. Some of them do come in handy, and help me to save a fair amount of time writing a solution from the ground up, or hunting something down at stack overflow.
If you liked this post maybe check out my many other posts on lodash, also if you have anything more to add I would like to hear from you as well, let me know whats up in the comments. In any case that you for reading.