# Large Sequences performing worse than large lists

**URL:** <https://discuss.kotlinlang.org/t/large-sequences-performing-worse-than-large-lists/3684>\
**Category:** Uncategorized\
**Created:** [July 10, 2017, 10:49pm UTC](https://discuss.kotlinlang.org/t/large-sequences-performing-worse-than-large-lists/3684 "2017-07-10T22:49:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simtse](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@simtse](https://discuss.kotlinlang.org/u/simtse)\
**Post date:** [July 10, 2017, 10:49pm UTC](https://discuss.kotlinlang.org/t/large-sequences-performing-worse-than-large-lists/3684/1 "2017-07-10T22:49:08Z")

</div>

So in the Kotlin in Action book, it talks about using Sequences when you have a large data set so you don’t incur the larger memory in dealing with each filter map function compared to using a standard List or Iterable.

In my gist [CollectionAndSequence.kt · GitHub](https://gist.github.com/simon-tse-hs/7dce88df4c0870b8d580205c587ea6ac) I wrote some code to average out over 1000 iterations of running filter map from 100-1,000,000 times  
I’m not sure why this is the case, but List came out on top for larger sets on average instead of smaller sets of 100.

`fun squareNumbers(range: CollectionRange): List<Int> = range.rangeList.filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }`

`fun squareNumbersSequence(range: CollectionRange) = range.rangeList.asSequence().filter { it % 3 == 0 }.map { it * it }.filter { it % 4 == 0 }.map { it * 3 }.filter { it % 4 == 0 }.toList()`

I did filter{}.map{}.filter{}.map{}.filter{} as the list of operations. Can someone shed some light why Sequences perform slower than lists for filter map compared to List Iterables?

---

<div class="post-metadata">

**Author:** ![Eliote](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/eliote/32/7934_2.png) [@Eliote](https://discuss.kotlinlang.org/u/Eliote)\
**Post date:** [July 11, 2017, 3:05pm UTC](https://discuss.kotlinlang.org/t/large-sequences-performing-worse-than-large-lists/3684/2 "2017-07-11T15:05:32Z")

</div>

The big diference is that the values of the Sequence are evaluated lazily so it does’t use a huge amount of memory. The performance of the List might be better but you can run out of memory easily depending on the amount/size of elements in the list.
