DEV Community

Discussion on: Understanding JavaScript Iteration with C#

Collapse
dance2die profile image
Sung M. Kim Author

Hi Miff.

ES6 makes it much easier to turn iterables into arrays using Array.from or spread syntax.

const iterable = document.querySelectAll('a');
Array.from(iterable).filter((a, i) => iterable[i - 1] !== a);
[...iterable].filter((a, i) => iterable[i - 1] !== a );

If you were to use only ES5 (for a personal preference or due to a business requirement) [].filter.call will come in handy.

At this point I am not sure about performance implications though.

Collapse
goaty92 profile image
goaty92 • Edited on

My bet is that performance implication will be on the bad side.
Both Array.from and spread syntax create a new array meaning they allocate extra memory. If your array size is small it 'might' be ok, but it might be a problem if you're processing large arrays

Thread Thread
dance2die profile image
Sung M. Kim Author

Sounds like a valid point there.

I'd still use a spray syntax over abstract [].filter.call as to promote readability (unless serious performance is gained)

Thread Thread
goaty92 profile image
goaty92

Sure, and if you use Typescript, with spray syntax the compiler can know about the type of the array and provide typing information.

If you're concerned with performance then manually iterating through the array is always the best option.