Multiplying 4*4 matrices I'm multiplying 4x4 matrices for learning purposes. How can I optimize this for performance?

HTML

&lt;table class="matrex-1"&gt;
    &lt;tr&gt;&lt;td class="0-0"&gt;-1&lt;/td&gt;&lt;td class="1-0"&gt;0&lt;/td&gt;&lt;td class="2-0"&gt;-1&lt;/td&gt;&lt;td class="3-0"&gt;0&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-1"&gt;8&lt;/td&gt;&lt;td class="1-1"&gt;-9&lt;/td&gt;&lt;td class="2-1"&gt;8&lt;/td&gt;&lt;td class="3-1"&gt;-9&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-2"&gt;5&lt;/td&gt;&lt;td class="1-2"&gt;6&lt;/td&gt;&lt;td class="2-2"&gt;2&lt;/td&gt;&lt;td class="3-2"&gt;-4&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-3"&gt;-2&lt;/td&gt;&lt;td class="1-3"&gt;7&lt;/td&gt;&lt;td class="2-3"&gt;-8&lt;/td&gt;&lt;td class="3-3"&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;table class="matrex-2"&gt;
    &lt;tr&gt;&lt;td class="0-0"&gt;4&lt;/td&gt;&lt;td class="1-0"&gt;3&lt;/td&gt;&lt;td class="2-0"&gt;4&lt;/td&gt;&lt;td class="3-0"&gt;3&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-1"&gt;-1&lt;/td&gt;&lt;td class="1-1"&gt;7&lt;/td&gt;&lt;td class="2-1"&gt;-1&lt;/td&gt;&lt;td class="3-1"&gt;7&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-2"&gt;5&lt;/td&gt;&lt;td class="1-2"&gt;2&lt;/td&gt;&lt;td class="2-2"&gt;9&lt;/td&gt;&lt;td class="3-2"&gt;-3&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-3"&gt;6&lt;/td&gt;&lt;td class="1-3"&gt;2&lt;/td&gt;&lt;td class="2-3"&gt;4&lt;/td&gt;&lt;td class="3-3"&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;table class="matrex-3"&gt;
    &lt;tr&gt;&lt;td class="0-0"&gt;&lt;/td&gt;&lt;td class="1-0"&gt;&lt;/td&gt;&lt;td class="2-0"&gt;&lt;/td&gt;&lt;td class="3-0"&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-1"&gt;&lt;/td&gt;&lt;td class="1-1"&gt;&lt;/td&gt;&lt;td class="2-1"&gt;&lt;/td&gt;&lt;td class="3-1"&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-2"&gt;&lt;/td&gt;&lt;td class="1-2"&gt;&lt;/td&gt;&lt;td class="2-2"&gt;&lt;/td&gt;&lt;td class="3-2"&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td class="0-3"&gt;&lt;/td&gt;&lt;td class="1-3"&gt;&lt;/td&gt;&lt;td class="2-3"&gt;&lt;/td&gt;&lt;td class="3-3"&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;


jQuery

var first, fifth,
    second, sixth,
    third, seventh,
    forth, eighth;

    for (var i = 0; i &lt; 4; i++) {                                                   
        for (var j = 0; j &lt; 4; j++) {
            first = parseInt($('.matrex-1 td.0-' + i).text());
            second = parseInt($('.matrex-2 td.' + j + '-0').text());
            third = parseInt($('.matrex-1 td.1-' + i).text());
            forth = parseInt($('.matrex-2 td.' + j + '-1').text());
            fifth = parseInt($('.matrex-1 td.2-' + i).text());
            sixth = parseInt($('.matrex-2 td.' + j + '-2').text());
            seventh = parseInt($('.matrex-1 td.3-' + i).text());
            eighth = parseInt($('.matrex-2 td.' + j + '-3').text());

            $('.matrex-3 td.' + j + '-' + i).text(first * second + third * forth + fifth * sixth + seventh * eighth);
        }
    }
For small matrix multiplications, I think it is typically fastest (surprisingly) to implement the basic matrix multiplication you learn in elementary/middle school, which gives us an asymptotic runtime complexity of n^3, where n is the length and width of the arrays (assuming both arrays have the same dimensions and aspect ratios). 

In Pseudo code, this algorithm goes as follows:

for (i = 0; i &lt; n; i++) {
        for (j = 0; j &lt; n; j++) {
            resultingArray[i][j] = 0;
            for (k = 0; k &lt; n; k++) {
                resultingArray[i][j] += firstArray[i][k] * secondArray[k][j];
            }
        }
    }
    return resultArray;


For larger matrices, typically 30 and up (subjective), there is an algorithm called Strassen's Algorithm that multiplies matrices with an approximate asymptotic runtime complexity of 2^n, again, where n is the length and width of the matrices. This problem is a recursive algorithm that breaks the problem into a lot of sub-problems by breaking down and working on web-matrices. This algorithm typically has a numeric "cross-over" point variable that is defined by the developer and represents the size bound of the matrices where it would be more efficient to simply multiply using the standard matrix multiplication algorithm. The algorithm would roughly go as follows:

if (n &lt;= crossOverPoint) {
        resultArray = basicMatrixMult (n, firstArray, secondArray, resultArray);
    } else {
        double[][] firstArraySubArray1 = partitionSubArray (firstArray, 0, (n / 2), 0, (n / 2));
        double[][] firstArraySubArray2 = partitionSubArray (firstArray, (n / 2), n, (n / 2), n);

        double[][] secondArraySubArray1 = partitionSubArray (secondArray, 0, (n / 2), 0, (n / 2));
        double[][] secondArraySubArray2 = partitionSubArray (secondArray, 0, (n / 2), (n / 2), n);

        resultArray = strassenMatrixMult ((n / 2), addMatrices (firstArraySubArray1, firstArraySubArray2),
                                                 addMatrices (firstArraySubArray1, firstArraySubArray1), 
                                                 resultArray);
    }
    return resultArray;


Here's some more info in case you wanted to read up on Strassen's Algorithm:
https://en.wikipedia.org/wiki/Strassen_algorithm