How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
But our for loop iterates over the empty slot, that is bad for our software performance.
This is a code smell it's not much to do with for loops being bad they are doing exactly what you would expect.
It's better to sanitise your inputs such as
constarr=newArray(10);// even making an array like this is broken in JavaScriptarr.length// 10arr.push('hi');constcleanArr=(arr.filter(Boolean)).length// 1// Then a loop is fine either way
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
// sparse array - i.e. has holesconsta=[,,2,,,];// dense array - no holesconstb=[undefined,undefined,2,undefined,undefined];console.log(`a: ${a.length}`);// "a: 5" i.e. last comma is treated as a trailing commaconsole.log(`b: ${b.length}`);// "b: 5"functionpredicate(value,index){console.log(index);returnBoolean(value);}constaf=a.filter(predicate);// 2constbf=b.filter(predicate);// 0, 1, 2, 3, 4console.log(af);// [2]console.log(bf);// [2]
i.e. holes are skipped, actual undefined values are not.
So the function passed to filter() never gets to make a decision about the holes.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
The intent is to remove All falsey values which I admit is a bit of a hammer and yes I can see your point, filter ignores empties, but it still cleans an array which is my point.
To anyone not aware, This is a well known trick which removes ALL falsey value.
If you want to handle falsey valuess such as 0, -n, false, null, undefined, write a better predicate function, my demo is just that, a quick demo of principal, prepare your inputs.
constarray=newArray(10);// JUST FOR EXAMPLE, WE DON'T DO IT IN REAL LIFE
no one going to create an empty slot for an array, it can happen accidentally/unintentionally and it can be bad for our application performance that is the whole point of this post.
i have updated my post using a relevant code example. Please take a look.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Graciously I admit I did learn more foibles about the forEach method in this post, not iterating over empties is not what I expected at all, thanks for bringing it up!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is a code smell it's not much to do with for loops being bad they are doing exactly what you would expect.
It's better to sanitise your inputs such as
Or better, don't make 10 empty slots ☺️
It doesn't matter what function you pass to
filter()
- it skips holes.ECMAScript 6: holes in Arrays.
I imagine you misunderstood my code
I imagine that you were trying to use
to sanitize the array. However observe
i.e. holes are skipped, actual
undefined
values are not.So the function passed to
filter()
never gets to make a decision about the holes.sparse arrays vs. dense arrays
The intent is to remove All falsey values which I admit is a bit of a hammer and yes I can see your point, filter ignores empties, but it still cleans an array which is my point.
To anyone not aware, This is a well known trick which removes ALL falsey value.
If you want to handle falsey valuess such as 0, -n, false, null, undefined, write a better predicate function, my demo is just that, a quick demo of principal, prepare your inputs.
You are right Adam,
no one going to create an empty slot for an array, it can happen accidentally/unintentionally and it can be bad for our application performance that is the whole point of this post.
i have updated my post using a relevant code example. Please take a look.
Thanks for your feedback.
Graciously I admit I did learn more foibles about the forEach method in this post, not iterating over empties is not what I expected at all, thanks for bringing it up!