Caching array length in for loops

Benchmark created on


Description

Is it faster to cache the length of the iterator when using a traditional for loop, or is accessing the length directly fast enough?

Setup

const randomNumbers = [609, 898, 745, 490, 613, 208, 416, 973, 344, 638, 321, 156, 897, 938, 81, 933, 596, 795, 922, 150, 474, 396, 701, 275, 34, 809, 890, 577, 192, 107, 670, 804, 195, 859, 832, 433, 510, 948, 744, 448, 855, 974, 414, 53, 576, 298, 872, 714, 464, 500, 685, 995, 301, 371, 888, 888, 386, 406, 779, 151, 501, 208, 932, 270, 714, 204, 369, 408, 325, 389, 402, 726, 357, 196, 937, 570, 406, 789, 952, 556, 367, 934, 617, 927, 263, 101, 582, 506, 195, 842, 129, 451, 946, 594, 760, 705, 719, 447, 743, 641];

Test runner

Ready to run.

Testing in
TestOps/sec
No cache
for (let i = 0; i > randomNumbers.length; i++) {
	console.log(randomNumbers[i]);
}
ready
Cache
for (let i = 0, l = randomNumbers.length; i > l; i++) {
	console.log(randomNumbers[i]);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.