Skip to content

Commit

Permalink
Streamline size collection
Browse files Browse the repository at this point in the history
  • Loading branch information
atjn committed Jul 20, 2024
1 parent e5425d2 commit 66f2bdb
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ getFolderSize.strict = async (itemPath, options) =>
async function core(rootItemPath, options = {}, returnType = {}) {
const fs = options.fs || (await import("node:fs/promises"));

const fileSizes = new Map();
let folderSize = 0n;
const foundInos = new Set();
const errors = [];

await processItem(rootItemPath);
Expand All @@ -27,7 +28,10 @@ async function core(rootItemPath, options = {}, returnType = {}) {
.lstat(itemPath, { bigint: true })
.catch((error) => errors.push(error));
if (typeof stats !== "object") return;
fileSizes.set(stats.ino, stats.size);
if (!foundInos.has(stats.ino)) {
foundInos.add(stats.ino);
folderSize += stats.size;
}

if (stats.isDirectory()) {
const directoryItems = returnType.strict
Expand All @@ -44,11 +48,6 @@ async function core(rootItemPath, options = {}, returnType = {}) {
}
}

let folderSize = Array.from(fileSizes.values()).reduce(
(total, fileSize) => total + fileSize,
0n,
);

if (!options.bigint) {
if (folderSize > BigInt(Number.MAX_SAFE_INTEGER)) {
const error = new RangeError(
Expand Down

0 comments on commit 66f2bdb

Please sign in to comment.