JavaScript is far from a familiar language to me. I have a piece of logic I am trying to optimise for speed. It consists in finding the argmax, row and columns index, of the a 2d array (rectangular shaped). At the moment, I have a naïve implementation
function argMax2d(arr) {
var rowMax = 0, colMax = 0;
for( var rowIndex = 0; rowIndex < arr.length; rowIndex++){
for( var colIndex = 0; colIndex < arr[rowIndex].length; colIndex++){
if (arr[rowIndex][colIndex] > arr[rowMax][colMax]){
rowMax = rowIndex;
colMax = colIndex;
}
}
}
return [rowMax, colMax];
}
In Python this would be a really slow way of getting the job done because of making no use of the contiguity of the data.
PS: arr
is always rectangular, the number of columns is the same in every row