Skip to content

Commit d3b1642

Browse files
committed
Added large object benchmark to benchmarks
1 parent 4801dde commit d3b1642

File tree

2 files changed

+81
-45
lines changed

2 files changed

+81
-45
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ There are three different types of changes. `CREATE`, `REMOVE`, and `CHANGE`. Th
3838
# Benchmarks
3939

4040
```
41-
deep-diff: 23054ns - 206% slower
42-
deep-object-diff: 30809ns - 309% slower
43-
jsdiff: 112154ns - 1389% slower
44-
microdiff: 7530ns - Fastest
41+
Benchmarks: Small object
42+
deep-diff: 17929ns - 409% slower
43+
deep-object-diff: 10763ns - 206% slower
44+
jsdiff: 79700ns - 2164% slower
45+
microdiff: 3520ns - Fastest
46+
47+
Benchmarks: Large Object
48+
deep-diff: 272887ns - 259% slower
49+
deep-object-diff: 160019ns - 111% slower
50+
jsdiff: 1688294ns - 2123% slower
51+
microdiff: 75934ns - Fastest
4552
```
4653

4754
These benchmarks are currently only for one small object, so they might not be accurate. I will be working on creating benchmarks with more varying types.

bench.js

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,79 @@ import { diffJson } from "diff";
44
import microdiff from "./dist/index.js";
55
import { hrtime } from "node:process";
66
import colors from "picocolors";
7+
const characters = "abcdefghijklmnopqrstuvwxyz1234567890".split("");
78

8-
const obj = {
9-
test: "test",
10-
testing: true,
11-
bananas: "awesome",
12-
bestFruits: ["bananas", "kiwi", "blueberries"],
13-
};
14-
const newObj = {
15-
test: "new test",
16-
testing: true,
17-
bananas: "awesome",
18-
bestFruits: ["bananas", "kiwi", "blueberries", "blackberries"],
19-
};
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]);
9+
async function benchmark(name, obj, newObj, exclude = []) {
10+
const benchmarks = {
11+
"deep-diff": () => deepDiff.diff(obj, newObj),
12+
"deep-object-diff": () => deepObjectDiff.detailedDiff(obj, newObj),
13+
jsdiff: () => diffJson(obj, newObj),
14+
microdiff: () => microdiff(obj, newObj),
15+
};
16+
let times = {};
17+
for (let benchmark in benchmarks) {
18+
if (exclude.includes(benchmark)) {
19+
continue;
20+
}
21+
times[benchmark] = [];
22+
for (let i = 1; i < 100; i++) {
23+
let time = hrtime();
24+
benchmarks[benchmark]();
25+
times[benchmark].push(hrtime(time)[1]);
26+
}
27+
times[benchmark] =
28+
times[benchmark].reduce((pv, nv) => pv + nv) / times[benchmark].length;
3329
}
34-
times[benchmark] =
35-
times[benchmark].reduce((pv, nv) => pv + nv) / times[benchmark].length;
30+
let output = [];
31+
let fastest = "";
32+
for (let time in times) {
33+
if (!fastest || times[time] < times[fastest]) {
34+
fastest = time;
35+
}
36+
}
37+
for (let time in times) {
38+
output.push(
39+
`${time}: ${Math.round(times[time])}ns - ${
40+
fastest === time
41+
? colors.bold(colors.green("Fastest"))
42+
: `${Math.round((times[time] / times[fastest] - 1) * 100)}% slower`
43+
}`
44+
);
45+
}
46+
console.log(
47+
colors.bold(colors.green(`Benchmarks: ${name}\n`)) + output.join("\n")
48+
);
3649
}
37-
let output = [];
38-
let fastest = "";
39-
for (let time in times) {
40-
if (!fastest || times[time] < times[fastest]) {
41-
fastest = time;
50+
51+
benchmark(
52+
"Small object (baseline)",
53+
{
54+
name: "Testing",
55+
propertyTwo: "Still testing...",
56+
},
57+
{
58+
name: "TestingChanged",
59+
propertyThree: "Still testing...",
60+
}
61+
);
62+
let largeObj = {};
63+
let i = 0;
64+
while (i < 300) {
65+
let randomString = "";
66+
for (let characterCount = 0; characterCount < 5; characterCount++) {
67+
randomString += characters[Math.round(Math.random() * characters.length)];
68+
}
69+
if (!largeObj[randomString]) {
70+
largeObj[randomString] = Math.random() * 100;
71+
i++;
4272
}
4373
}
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-
);
74+
let newLargeObj = {};
75+
for (let randomProperty in largeObj) {
76+
if (Math.random() > 0.95) {
77+
newLargeObj[randomProperty] = Math.random() * 100;
78+
} else if (!Math.random() < 0.975) {
79+
newLargeObj[randomProperty] = largeObj[randomProperty];
80+
}
5281
}
53-
console.log(colors.bold(colors.green("Benchmarks\n")) + output.join("\n"));
82+
benchmark("Large Object (300 properties)", largeObj, newLargeObj);

0 commit comments

Comments
 (0)