I'm multiplying 4x4 matrices for learning purposes. How can I optimize this for performance?HTML<table class=matrex-1>    <tr><td class=0-0>-1</td><td class=1-0>0</td><td class=2-0>-1</td><td class=3-0>0</td></tr>    <tr><td class=0-1>8</td><td class=1-1>-9</td><td class=2-1>8</td><td class=3-1>-9</td></tr>    <tr><td class=0-2>5</td><td class=1-2>6</td><td class=2-2>2</td><td class=3-2>-4</td></tr>    <tr><td class=0-3>-2</td><td class=1-3>7</td><td class=2-3>-8</td><td class=3-3>1</td></tr></table><table class=matrex-2>    <tr><td class=0-0>4</td><td class=1-0>3</td><td class=2-0>4</td><td class=3-0>3</td></tr>    <tr><td class=0-1>-1</td><td class=1-1>7</td><td class=2-1>-1</td><td class=3-1>7</td></tr>    <tr><td class=0-2>5</td><td class=1-2>2</td><td class=2-2>9</td><td class=3-2>-3</td></tr>    <tr><td class=0-3>6</td><td class=1-3>2</td><td class=2-3>4</td><td class=3-3>1</td></tr></table><hr><table class=matrex-3>    <tr><td class=0-0></td><td class=1-0></td><td class=2-0></td><td class=3-0></td></tr>    <tr><td class=0-1></td><td class=1-1></td><td class=2-1></td><td class=3-1></td></tr>    <tr><td class=0-2></td><td class=1-2></td><td class=2-2></td><td class=3-2></td></tr>    <tr><td class=0-3></td><td class=1-3></td><td class=2-3></td><td class=3-3></td></tr></table>jQueryvar first, fifth,    second, sixth,    third, seventh,    forth, eighth;    for (var i = 0; i < 4; i++) {                                                           for (var j = 0; j < 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);        }    }
Multiplying 4*4 matrices
javascript;performance;jquery;matrix
While the other guy did the math, I'll do the web part.It would be better to collect all your values first then do the operation. Accessing the DOM is expensive. Doing it n^2 times would be very slow. Using jQuery inside n^2 is even terrible as jQuery parses your selector, traverses the DOM, wraps it in a jQuery object, and all the browser-compatibility stuff without you noticing it. You can use any of the available native methods to retrieve the values.Depending on your math, you can represent your matrix as a 1D array (a 4x4 would be an array of 16 in length), or a 2D nested array(a 4x4 would be an array of 4 arrays, each with 4 items). A 1D array will save you memory at the cost of computation, while a 2D array is simpler to visualize, but you'll use up at least n+1 arrays.[1,2,3,4,5,6,7,8,9,...,16].// or[  [1,2,3,4],  [5,6,7,8],  ...  [.,.,.,16]]For parseInt, you'll need to provide the radix as a second parameter. Otherwise, if you omit it, it will assume that your strings are in octal and not decimal. Your math might be off. Additionally, if parseInt encounters a non-numeric character first, it returns NaN. Then all your math will result in NaN. Always check for the results of parseInt.