Watch “Array reduce vs chaining vs for loop” on egghead.io
I have been within the means of transferring a few of my digital life round and one factor
that I’ve needed to do is obtain all of my pictures from Google Images. Due to
the best way these are organized, I discovered the necessity to rearrange them, so I wrote a
little node script to do it. What the script does shouldn’t be solely related for
this submit so I am not going to enter element, (however
here’s the whole thing
if you happen to wanna give it a read-through). Here is the bit I wish to speak about
(edited barely for readability):
const strains = execSync(`discover "$searchPath" -type f`).toString().break up('n')
const instructions = strains
.map(f => f.trim())
.filter(Boolean)
.map(file =>
const destFile = getDestFile(file)
const destFileDir = path.dirname(destFile)
return `mkdir -p "$destFileDir" && mv "$file" "$destFile"`
)
instructions.forEach(command => execSync(command))
Principally all this does is makes use of the linux discover
command to discover a record of recordsdata
in a listing, then separate the outcomes of that script into strains, trim them
to do away with whitespace, take away empty strains, then map these to instructions to maneuver
these recordsdata, after which run these instructions.
I shared the script
on twitter and had
a number of folks critiquing the script and recommend that I might have used cut back
as an alternative. I am fairly certain each of them have been suggesting it as a efficiency
optimization as a result of you may cut back (no pun supposed) the variety of occasions
JavaScript has to loop over the array.
Now, to be clear, there have been about 50 thousand objects on this array, so
undoubtedly quite a lot of dozen you cope with in typical UI growth, however I
wish to first make some extent that in a state of affairs like one-off scripts that you just run
as soon as and you then’re carried out, efficiency ought to principally be the very last thing to
fear about (until what you are doing actually is tremendous costly). In my case, it
ran loads quick. The sluggish half wasn’t iterating over the array of components
a number of occasions, however
running the commands.
A number of different folks steered that I exploit Node APIs and even open supply modules
from npm to assist run these scripts as a result of it might “most likely be sooner and work
cross platform.” Once more, they’re most likely not improper, however for one-off scripts that
are “quick sufficient”, these issues do not matter. This can be a basic instance of
making use of irrelevant constraints on an issue leading to a extra sophisticated
resolution.
In any case, I did wish to handle the concept of utilizing cut back
as an alternative of the
map
, filter
, then map
I’ve happening there.
Here is what that very same code can be like if we use cut back
const instructions = strains.cut back((accumulator, line) =>
let file = line.trim()
if (file)
const destFile = getDestFile(file)
const destFileDir = path.dirname(destFile)
accumulator.push(`mkdir -p "$destFileDir" && mv "$file" "$destFile"`)
return accumulator
, [])
Now, I am not a type of individuals who suppose that
reduce
is the spawn of the evil one
(checkout that thread for attention-grabbing examples of cut back), however I do really feel like I
can acknowledge when code is definitely easier/extra advanced and I might say that the
cut back instance right here is certainly extra advanced than the chaining instance.
Actually, I have been utilizing array strategies so lengthy, I am going to want a second to rewrite
this as a for loop. So… one sec…
Okay, right here you go:
const instructions = []
for (let index = 0; index < strains.size; index++)
const line = strains[index]
const file = line.trim()
if (file)
const destFile = getDestFile(file)
const destFileDir = path.dirname(destFile)
instructions.push(`mkdir -p "$destFileDir" && mv "$file" "$destFile"`)
That is not a lot easier both.
EDIT: BUT WAIT! We will simplify this due to for..of
!
const instructions = []
for (const line of strains)
const file = line.trim()
if (!file) proceed
const destFile = getDestFile(file)
const destFileDir = path.dirname(destFile)
instructions.push(`mkdir -p "$destFileDir" && mv "$file" "$destFile"`)
Actually I do suppose that is not an entire lot higher than the normal loop, however
I do suppose it is fairly easy. I feel some folks low cost for loops as a result of
they’re “crucial”, after they’re really fairly helpful.
Typically, I will be selecting between chaining and for..of
loops. If I’ve
a efficiency concern with iterating over the array a number of occasions, then
for..of
will certainly be my collection of selection.
I do not usually use cut back
, however generally I am going to strive it out and evaluate it to
different choices and go together with that. I notice how subjective that sounds, however so
a lot of coding is subjective so
I might have an interest to listen to what you suppose. Reply to the tweet beneath and let me
know. And be happy to retweet if that is one thing you are into.
#Array #cut back #chaining #loop