Skip to content

Commit 95b5f4d

Browse files
committed
refactor: optimize to2DArray method to use Float64Array and improve performance
1 parent 22a49a1 commit 95b5f4d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ export class SparseMatrix {
6464
}
6565

6666
/**
67-
* @returns {number[][]}
67+
* @returns {Float64Array[]}
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+
Float64Array.from({ length: this.columns }),
72+
);
73+
this.withEachNonZero((i, j, v) => {
74+
copy[i][j] = v;
75+
});
76+
7777
return copy;
7878
}
7979

0 commit comments

Comments
 (0)