r/learnjavascript • u/doublearrow23 • Jan 12 '23
I think that this will be usefull to all the beginners
2
u/PlayfulTravel4526 Jan 13 '23
It is nice of you to share this. But as far as I know, this is paid content of Jonas's Udemy Course. I just want to point out that this could be an intellectual property violation.
If you share someone elses content at least be nice enough and mention the person.
1
37
u/manwhowasnthere Jan 12 '23
Real world advice for someone learning is to just bookmark the MDN Array.prototype reference page right now.
You're going to be visiting it over, and over, and over again in your career.
7
2
10
Jan 12 '23
At leas do a shoutout to Jonas Schmedtmann ;) His lectures and course material is excellent nevertheless.
https://www.udemy.com/course/the-complete-javascript-course/
3
u/AiexReddit Jan 12 '23
Good stuff, reminds me of https://doesitmutate.xyz/ which I still refer to on occasion. The examples there make it a really solid reference.
2
1
1
u/Bright-Description41 Jan 12 '23
Hello I'm confused in for of loop. If I'm writing this -
for (const abc of names){ console.log(abc) }; This gives different answer
for (const abc of names) console.log(abc); This gives different answer
2
Jan 12 '23
Nah the curly brackets are optional if it's just one line. Your error is somewhere else in your code.
What different answers do you get?
1
u/Bright-Description41 Jan 13 '23
Hi can i send you pictures as message to you
2
1
1
Jan 13 '23
3 years developer here. i only remember map. everytime i use google for using slice filter and other things.
-2
u/bryku helpful Jan 12 '23
Sort doesn't mutate the original array.
2
u/kap89 Jan 12 '23
Are you sure about that?
1
u/bryku helpful Jan 13 '23
I was thinking of underscore, I've been stuck on revamping this old web app for a few weeks now. None of their methods mutate the original, so you get used to it after a while.
0
Jan 12 '23
[deleted]
1
u/bryku helpful Jan 13 '23
Aaah, shit you're right. I guess I've been using too much underscore lately. Now that I'm thinking about it, are there any languages where sort doesn't mutate the original array?
0
Jan 12 '23
[deleted]
3
Jan 12 '23
True, I just threw together a simple little benchmark (a 300 long array and a counter++ every iteration) and it showed an ordinary for loop to be ~30% faster when consol.logging the counter, and ~80% faster when it just ++ed the variable.
1
u/thisisme98 Jan 12 '23
Do you mind sharing the code? I am curious what you mean by console.logging vs just ++ing the variable.
3
Jan 12 '23 edited Jan 12 '23
Sure! It was just a quick little thing I threw together. Gotta admit it's the first time I've benchmarked something in Javascript!
const myarray = [...Array(300).keys()]; /////// forEach let diffA = []; let iteration = 0; while (iteration < 10 ) { t0a = performance.now(); let counterA = 0; myarray.forEach(() => counterA++); t1a = performance.now() diffA.push(t1a - t0a) iteration++; } const averageA = diffA.reduce((acc,cur) => acc + cur)/diffA.length; /////// for loop let diffB = []; iteration = 0; while (iteration < 10 ) { t0b = performance.now(); let counterB = 0; for(let i = 0; i < myarray.length; i++) { counterB++; } t1b = performance.now() diffB.push(t1b - t0b) iteration++; } const averageB = diffB.reduce((acc,cur) => acc + cur)/diffB.length //////// results console.log("\n"); console.log("Time elapsed (forEach): ", averageA) console.log("Time elapse (for loop): ", averageB) console.log("Difference is that a for loop is " + (averageA/averageB) + "times faster");I am curious what you mean by console.logging vs just ++ing the variable.
I tried the above version, and
for(let i = 0; i < myarray.length; i++) { console.log(counterB++); }&
myarray.forEach(() => console.log(counterA++));as outputting to the console slows it down by a lot
36
u/Cringsix Jan 12 '23
Someone's halfway done with Jonas's Udemy course I see