Skip to content

Commit

Permalink
perf(ipc): skip drain
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Sep 18, 2023
1 parent 3113f02 commit e5861a2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
## motivation

I want to know the folder size quickly, but the nodejs implementation of
[get-folder-size](https://github.com/alessioalex/get-folder-size) is slow, so using
go implements a recursive get folder size that runs in nodejs and is `7 ~ 20` times faster than the node native solution under normal circumstances。
[get-folder-size](https://github.com/alessioalex/get-folder-size) is slow, so
using go implements a recursive get folder size that runs in nodejs and is
`7 ~ 20` times faster than the node native solution under normal circumstances。

<br />

Expand Down
3 changes: 2 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

想要快速知道文件夹大小,但 nodejs 实现的
[get-folder-size](https://github.com/alessioalex/get-folder-size) 是慢的,所以用
go 实现了递归获取文件夹大小,能跑在 nodejs 中,正常情况下,比 node 原生方案快 `7 ~ 20`
go 实现了递归获取文件夹大小,能跑在 nodejs 中,正常情况下,比 node 原生方案快
`7 ~ 20`

具体可见 issue 👉
[get-folder-size/issues/22](https://github.com/alessioalex/get-folder-size/issues/22)
Expand Down
2 changes: 1 addition & 1 deletion bench.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

const { getFolderSizeWithIpc, close } = createGetFolderSizeBinIpc();

const base = "./";
const base = "../";

const nodeStartTime = Date.now();
const nodeResult = await getFolderSize(base, true);
Expand Down
15 changes: 7 additions & 8 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function getFolderSizeBin(
});

if (stderr) {
throw new Error(stderr)
throw new Error(stderr);
}

if (pretty) {
Expand All @@ -95,9 +95,7 @@ export async function getFolderSizeBin(
return Number(stdout);
}

export function createGetFolderSizeBinIpc(
options: Options = {},
) {
export function createGetFolderSizeBinIpc(options: Options = {}) {
const { binPath = detectDefaultBinPath(), loose = false } = options;

let tasks = new Map<
Expand Down Expand Up @@ -128,17 +126,18 @@ export function createGetFolderSizeBinIpc(
let full = false;
function send(base: string) {
if (full) {
return go.stdin.once("drain", () => go.stdin.write(`${base},`));
return go.stdin.once("drain", () => {
full = false;
go.stdin.write(`${base},`);
});
}
full = !go.stdin.write(`${base},`);
}

readline.on("line", (item: string) => {
const [base, size] = item.split(",");
const { pretty, resolve } = tasks.get(base);
resolve(
pretty ? prettyBytes(Number(size)) : Number(size),
);
resolve(pretty ? prettyBytes(Number(size)) : Number(size));
tasks.delete(base);
});

Expand Down
Binary file modified wasm/main.wasm
Binary file not shown.

0 comments on commit e5861a2

Please sign in to comment.