And WOW, creating the array at the correct size really makes a huge difference:
function buildSquareMatrix2(items) { const len = items.length let matrix = new Array(len) for (let i = 0; i < len; i++) { const next = new Array(len) for (let j = 0; j < len; j++) { next[j] = items[j] } matrix[i] = next } return matrix }
buildSquareMatrix 9504 buildSquareMatrix2 1528 buildSquareMatrix3 678
Less than 3x slowdown over .slice() while still allowing you to modify each value!
.slice()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
And WOW, creating the array at the correct size really makes a huge difference:
Less than 3x slowdown over
.slice()
while still allowing you to modify each value!