COMMUNITY

Array elements iteration performance

Hi!

I have a question: is there any benefit in performance when using

for(elem in array) some_func(elem);

instead of

for(i in 0...array.length) some_func(array[i]);

From what I can see, in JavaScript it is generating the same code, so there should be no difference in performance.
https://try.haxe.org/#3F7B7

This Haxe code

function some_func(el) trace(el);
var array = [2,4,6,8,10];

trace("loop1");
for(elem in array) some_func(elem);

trace("loop2");
for(i in 0...array.length) some_func(array[i]);

Generates this JavaScript

// Generated by Haxe 3.4.4
(function () { "use strict";
var Test = function() { };
Test.main = function() {
	var some_func = function(el) {
		console.log(el);
	};
	var array = [2,4,6,8,10];
	console.log("loop1");
	var _g = 0;
	while(_g < array.length) {
		var elem = array[_g];
		++_g;
		some_func(elem);
	}
	console.log("loop2");
	var _g1 = 0;
	var _g2 = array.length;
	while(_g1 < _g2) some_func(array[_g1++]);
};
Test.main();
})();

Thanks for clarifying!