chakra-core / chakra-core/ChakraCore

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

Open
#6,711 15 comments 0 reactions 0 assignees View on GitHub
Performance
Dominant language
JavaScript
Stars
9.3k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

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:
```js
{
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)');
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.