Skip to content

Commit 1536150

Browse files
committed
Improved benchmarks
1 parent fa70e05 commit 1536150

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

bench.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import deepDiff from "deep-diff";
22
import deepObjectDiff from "deep-object-diff";
3+
import { diffJson } from "diff";
34
import microdiff from "./dist/index.js";
5+
import { hrtime } from "node:process";
6+
import colors from "picocolors";
7+
48
const obj = {
59
test: "test",
610
testing: true,
@@ -13,12 +17,37 @@ const newObj = {
1317
bananas: "awesome",
1418
bestFruits: ["bananas", "kiwi", "blueberries", "blackberries"],
1519
};
16-
console.time("deep-diff");
17-
deepDiff.diff(obj, newObj);
18-
console.timeEnd("deep-diff");
19-
console.time("deep-object-diff");
20-
deepObjectDiff.detailedDiff(obj, newObj);
21-
console.timeEnd("deep-object-diff");
22-
console.time("microdiff");
23-
microdiff(obj, newObj);
24-
console.timeEnd("microdiff");
20+
const benchmarks = {
21+
"deep-diff": () => deepDiff.diff(obj, newObj),
22+
"deep-object-diff": () => deepObjectDiff.detailedDiff(obj, newObj),
23+
jsdiff: () => diffJson(obj, newObj),
24+
microdiff: () => microdiff(obj, newObj),
25+
};
26+
let times = {};
27+
for (let benchmark in benchmarks) {
28+
times[benchmark] = [];
29+
for (let i = 1; i < 100; i++) {
30+
let time = hrtime();
31+
benchmarks[benchmark]();
32+
times[benchmark].push(hrtime(time)[1]);
33+
}
34+
times[benchmark] =
35+
times[benchmark].reduce((pv, nv) => pv + nv) / times[benchmark].length;
36+
}
37+
let output = [];
38+
let fastest = "";
39+
for (let time in times) {
40+
if (!fastest || times[time] < times[fastest]) {
41+
fastest = time;
42+
}
43+
}
44+
for (let time in times) {
45+
output.push(
46+
`${time}: ${Math.round(times[time])}ns - ${
47+
fastest === time
48+
? colors.bold(colors.green("Fastest"))
49+
: `${Math.round((times[time] / times[fastest] - 1) * 100)}% slower`
50+
}`
51+
);
52+
}
53+
console.log(colors.bold(colors.green("Benchmarks\n")) + output.join("\n"));

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"module": "./dist/index.js",
77
"types": "./dist/index.d.ts",
88
"scripts": {
9-
"build": "tsc",
10-
"test": "tsc && uvu tests",
11-
"bench": "tsc && node bench.js",
12-
"prepublish": "tsc"
9+
"build": "tsc && prettier -w dist/*",
10+
"test": "npm run build && uvu tests",
11+
"bench": "npm run build && node bench.js",
12+
"prepublish": "npm run build"
1313
},
1414
"keywords": [
1515
"diff",
@@ -23,6 +23,8 @@
2323
"devDependencies": {
2424
"deep-diff": "^1.0.2",
2525
"deep-object-diff": "^1.1.0",
26+
"diff": "^5.0.0",
27+
"picocolors": "^1.0.0",
2628
"prettier": "^2.4.1",
2729
"typescript": "^4.4.4",
2830
"uvu": "^0.5.2"

0 commit comments

Comments
 (0)