3 ms·
As of ES6 I would limit myself to only three styles of writing array for-loops: 1. Crockford's C style with cached array length (fast and flexible, but verbose
by jarek-foksa 13y ago
As of ES6 I would limit myself to only three styles of writing array for-loops:
1. Crockford's C style with cached array length (fast and flexible, but verbose):
for (let index = 0, length = array.length; index < length; index += 1) {
console.log(index, array[index]);
}
2. Functional style (terse and flexible, but slow):
array.forEach((value, index) => console.log(index, value));
3. Python style (fast and terse, but not flexible):
for (let value of array) {
console.log(value);
}
By "flexible" I mean being able to e.g. iterate over items with odd indexes or in reverse direction.