// Shifting the array and accessing 0
let sum = 0;
while(matrix.length > 0) {
sum += matrix[0][0];
matrix.shift();
}
// direct access
let sum = 0;
for (let i = 0; i < matrix.length; i++) {
sum += matrix[i][0];
}
https://jsperf.com/shift-vs-index-access
Shifting the array and accessing 0 is faster than direct access in the given examples in the above jsPerf link.
Isn't shift()
an O(n)
operation?