Skip to content

[Perfomance] For-of vs For non-cached vs For-cached #6711

@MadProbe

Description

@MadProbe

After running small benchmark on Windows 10 with x64_debug config, i've got these results:

13996ms (for-of), 90ms (for-non-cached), 63ms (for-cached) (on average after 10 subsequent runs)
For-of is slower than for-non-cached by 15551%
and For-of is slower than for-cached by 22215%!

Bench code:

{
    const a = Array.from({ length: 1e7 }, _ => 0);
    let start = (console.log('Start'), Date.now());
    for (const _ of a);
    console.log(Date.now() - start + 'ms (for-of)');
    start = Date.now();
    for (let i = 0; i < a.length; i++) { const _ = a[i]; }
    console.log(Date.now() - start + 'ms (for-non-cached)');
    start = Date.now();
    const length = a.length;
    for (let i = 0; i < length; i++) { const _ = a[i]; }
    console.log(Date.now() - start + 'ms (for-cached)');
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions