Skip to content

Commit e80f453

Browse files
authored
perf: optimize to2DArray method by only getting non-zeros (#19)
1 parent 22a49a1 commit e80f453

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ export class SparseMatrix {
6767
* @returns {number[][]}
6868
*/
6969
to2DArray() {
70-
const copy = new Array(this.rows);
71-
for (let i = 0; i < this.rows; i++) {
72-
copy[i] = new Array(this.columns);
73-
for (let j = 0; j < this.columns; j++) {
74-
copy[i][j] = this.get(i, j);
75-
}
76-
}
70+
const copy = Array.from({ length: this.rows }, () =>
71+
new Array(this.columns).fill(0),
72+
);
73+
this.withEachNonZero((i, j, v) => {
74+
copy[i][j] = v;
75+
});
76+
7777
return copy;
7878
}
7979

0 commit comments

Comments
 (0)