Skip to main content

Timeline for Loop through an array in JavaScript

Current License: CC BY-SA 4.0

26 events
when toggle format what by license comment
Nov 13, 2022 at 21:52 history edited Kamil Kiełczewski CC BY-SA 4.0
new tests
Mar 1, 2021 at 5:21 comment added Kamil Kiełczewski @Pitouli you are right - I rollback answer to its initial form. When I have more time then I will perform benchamarks again
Mar 1, 2021 at 5:17 history rollback Kamil Kiełczewski
Rollback to Revision 1
Mar 1, 2021 at 3:52 comment added Pitouli Careful! The benchmark is wrong, because the array is not reset between each execution. Since the shift() has emptied the array after the first execution, all the subsequent executions are indeed extremely fast :p When you correctly reset the array, it appears that this solution is the second slowest. jsbench.me/4dklq1kjef/1
Nov 9, 2020 at 20:03 comment added Arthur S This isn't esoteric. Its simply unnecessary and makes an assumption that the a variable will not be used in further code.
Oct 30, 2020 at 8:35 comment added Peter Mortensen Even if it does not exist in your native language, you should not leave out articles in English (the indefinite article ("a" or "an") and the definite article ("the")). See e.g. English Articles - 3 Simple Rules To Fix Common Grammar Mistakes & Errors and A, AN, THE - Articles in English.
Oct 30, 2020 at 8:33 history edited Peter Mortensen CC BY-SA 4.0
Active reading [<https://www.youtube.com/watch?v=1Dax90QyXgI&t=17m54s> <https://www.youtube.com/watch?v=1Dax90QyXgI&t=19m05s>].
Aug 9, 2020 at 7:26 history edited Kamil Kiełczewski CC BY-SA 4.0
deleted 10 characters in body
Aug 6, 2020 at 8:07 history edited Kamil Kiełczewski CC BY-SA 4.0
edited body
Aug 6, 2020 at 8:01 history edited Kamil Kiełczewski CC BY-SA 4.0
added 351 characters in body
Aug 5, 2020 at 22:11 history edited Kamil Kiełczewski CC BY-SA 4.0
added 180 characters in body
Aug 5, 2020 at 21:47 history edited Kamil Kiełczewski CC BY-SA 4.0
deleted 2 characters in body
Aug 5, 2020 at 7:05 history edited Kamil Kiełczewski CC BY-SA 4.0
added 2 characters in body
Aug 5, 2020 at 6:30 history edited Kamil Kiełczewski CC BY-SA 4.0
deleted 5 characters in body
Aug 5, 2020 at 6:13 history edited Kamil Kiełczewski CC BY-SA 4.0
deleted 5 characters in body
Aug 4, 2020 at 21:29 history edited Kamil Kiełczewski CC BY-SA 4.0
added 2 characters in body
Aug 4, 2020 at 21:23 comment added Kamil Kiełczewski @Sapphire_Brick - thank you for your comment - I tink using console.log in tests is not good because it is complex system function and have big impact on run time. I check with r+=a[i].length (sum of words length) for array witch 1000 elements - and still this solution was much faster than other solution (and probaly the speed difference grow when number of elements grow...) - I'm surprised too that this solution is so fast :)
Aug 4, 2020 at 21:18 history edited Kamil Kiełczewski CC BY-SA 4.0
deleted 4 characters in body
Aug 4, 2020 at 21:11 history edited Kamil Kiełczewski CC BY-SA 4.0
added 231 characters in body
Aug 4, 2020 at 21:02 history edited Kamil Kiełczewski CC BY-SA 4.0
added 231 characters in body
Aug 4, 2020 at 20:51 comment added Sapphire_Brick You have make a good point. I ran your example with an array of 1000 items, and while(a.length) { console.log(a.shift()); } was about twice as fast as the for(var i = 0; i < a.length; i++) { console.log(a[i]); } version. ¯\_(ツ)_/¯
Jul 24, 2020 at 22:04 comment added Kamil Kiełczewski @Sapphire_Brick don't be lazy! :) you can check it your self- I give you link (test) to tool in above comment. However for small array case (as OP uses) when you need to optimize speed you should choose solution fastest for small arrays (not for big arrays - which can be different)
Jul 24, 2020 at 21:24 comment added Sapphire_Brick Of course a simple program like this is fast, but how does it scale in comparison to for(...;...;...) or for(... of ...)?
Jul 24, 2020 at 10:06 comment added Kamil Kiełczewski @Sapphire_Brick actually it is quite fast - here is test
Jan 16, 2020 at 2:49 comment added Sapphire_Brick that's the Haskell-y way to do it; keep taking the first one. clever, but probably slow.
Dec 19, 2019 at 6:40 history answered Kamil Kiełczewski CC BY-SA 4.0