From 95b5f4d19e47f9f150748beda11e5a808feeaf5f Mon Sep 17 00:00:00 2001 From: jobo322 Date: Mon, 7 Jul 2025 08:55:56 -0500 Subject: [PATCH 1/3] refactor: optimize to2DArray method to use Float64Array and improve performance --- src/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 6c23c82..8b0223d 100644 --- a/src/index.js +++ b/src/index.js @@ -64,16 +64,16 @@ export class SparseMatrix { } /** - * @returns {number[][]} + * @returns {Float64Array[]} */ to2DArray() { - const copy = new Array(this.rows); - for (let i = 0; i < this.rows; i++) { - copy[i] = new Array(this.columns); - for (let j = 0; j < this.columns; j++) { - copy[i][j] = this.get(i, j); - } - } + const copy = Array.from({ length: this.rows }, () => + Float64Array.from({ length: this.columns }), + ); + this.withEachNonZero((i, j, v) => { + copy[i][j] = v; + }); + return copy; } From a80cfea33c9cd258e9b54d155a67bbf3c5435752 Mon Sep 17 00:00:00 2001 From: jobo322 Date: Mon, 7 Jul 2025 08:57:24 -0500 Subject: [PATCH 2/3] chore: keep output typing --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 8b0223d..160fa09 100644 --- a/src/index.js +++ b/src/index.js @@ -64,11 +64,11 @@ export class SparseMatrix { } /** - * @returns {Float64Array[]} + * @returns {number[][]} */ to2DArray() { const copy = Array.from({ length: this.rows }, () => - Float64Array.from({ length: this.columns }), + Array.from({ length: this.columns }, () => 0), ); this.withEachNonZero((i, j, v) => { copy[i][j] = v; From cc9de1e93522f4550700ca8d625f8ec35838b873 Mon Sep 17 00:00:00 2001 From: jobo322 Date: Mon, 7 Jul 2025 16:55:16 -0500 Subject: [PATCH 3/3] chore: avoid the use of callbacks to fill the rows --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 160fa09..22ceb43 100644 --- a/src/index.js +++ b/src/index.js @@ -68,7 +68,7 @@ export class SparseMatrix { */ to2DArray() { const copy = Array.from({ length: this.rows }, () => - Array.from({ length: this.columns }, () => 0), + new Array(this.columns).fill(0), ); this.withEachNonZero((i, j, v) => { copy[i][j] = v;