Skip to content

Commit daf06fe

Browse files
committed
feat: use workers for streamListDiff
1 parent 4d22c68 commit daf06fe

21 files changed

+3040
-236
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/node_modules
22
dist
3-
.eslintcache
3+
.eslintcache
4+
# Ignore generated worker files
5+
src/lib/stream-list-diff/server/node-worker.cjs

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ type ObjectDiff = {
104104
diff: Diff[];
105105
};
106106

107-
/** recursive diff in case of subproperties */
108107
type Diff = {
109108
property: string;
110109
previousValue: unknown;
111110
currentValue: unknown;
112111
status: "added" | "deleted" | "equal" | "updated";
112+
// recursive diff in case of subproperties
113113
diff?: Diff[];
114114
};
115115
```
@@ -314,6 +314,8 @@ import { streamListDiff } from "@donedeal0/superdiff/client";
314314

315315
Streams the diff of two object lists, ideal for large lists and maximum performance.
316316

317+
ℹ️ `streamListDiff` requires ESM support. For browser usage, it will work out of the box if you use a modern bundler (Webpack, Rollup) or JavaScript framework (Next.js, Vue.js). For Node.js, just add `"type": "module"` in your package.json.
318+
317319
#### FORMAT
318320

319321
**Input**
@@ -330,6 +332,7 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
330332
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
331333
chunksSize?: number, // 0 by default
332334
considerMoveAsUpdate?: boolean; // false by default
335+
useWorker?: boolean // true by default
333336
}
334337
```
335338

@@ -344,7 +347,9 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
344347
options: {
345348
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
346349
chunksSize?: number, // 0 by default
347-
considerMoveAsUpdate?: boolean; // false by default
350+
considerMoveAsUpdate?: boolean, // false by default
351+
useWorker?: boolean // true by default
352+
348353
}
349354
```
350355

@@ -355,6 +360,9 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
355360
- `chunksSize` the number of object diffs returned by each streamed chunk. (e.g. `0` = 1 object diff per chunk, `10` = 10 object diffs per chunk).
356361
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
357362
- `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`.
363+
- `useWorker`: if set to `true`, the diff will be run in a worker for maximum performance. Only recommended for large lists (e.g. +100,000 items).
364+
365+
> ⚠️ Warning: using Readable streams in NodeJS may impact workers' performance since they need to be converted to arrays. Consider using arrays or files for optimal performance. Alternatively, you can turn the `useWorker` option off.
358366
359367
**Output**
360368

eslint.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import tseslint from "typescript-eslint";
33

44
export default [
55
{ files: ["**/*.{js,mjs,cjs,ts}"] },
6-
{ ignores: ["dist", "jest.config.js"] },
6+
{ ignores: ["dist", "jest.config.js", "src/lib/stream-list-diff/server/node-worker.cjs"] },
77
{ settings: { react: { version: "detect" } } },
88
pluginJs.configs.recommended,
99
...tseslint.configs.recommended,

jest.config.js renamed to jest.config.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
module.exports = {
1+
import type { Config } from "jest";
2+
3+
const config: Config = {
24
transform: {
3-
"^.+\\.(ts|js)$": [
5+
"^.+\\.(ts|js)$": [
46
"@swc/jest",
57
{
68
jsc: {
@@ -13,11 +15,14 @@ module.exports = {
1315
paths: {
1416
"@models/*": ["./src/models/*"],
1517
"@lib/*": ["./src/lib/*"],
16-
1718
},
1819
target: "esnext",
1920
},
2021
},
2122
],
2223
},
24+
testEnvironment: "node",
25+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
2326
};
27+
28+
export default config;

jest.setup.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TextEncoder, TextDecoder } from "util";
2+
3+
global.TextEncoder = TextEncoder;
4+
//@ts-expect-error - the TextDecoder is valid
5+
global.TextDecoder = TextDecoder;

0 commit comments

Comments
 (0)