Why is using a generator function slower than filling and iterating an array in this example?

后端 未结 4 1493
温柔的废话
温柔的废话 2021-02-12 21:26

A Tale of Two Functions

I have one function that fills an array up to a specified value:

function getNumberArray(maxValue) {
    const a         


        
4条回答
  •  无人及你
    2021-02-12 21:27

    In fact, running this benchmark now, generators at ~2x faster.

    I've modified the code slightly (moved let i) and here is the full gist: https://gist.github.com/antonkatz/6b0911c85ddadae39c434bf8ac32b340

    On my machine, these are the results:

    Running Array... Total time: 4,022ms Rss: 2,728,939,520 Heap Total: 2,726,199,296 Heap Used: 2,704,236,368

    Running Generator... Total time: 2,541ms Rss: 851,968 Heap Total: 0 Heap Used: -5,073,968

    I was very curious myself and could not find a proper answer. Thanks @David for providing the test code.

提交回复
热议问题