-
Notifications
You must be signed in to change notification settings - Fork 77
/
bench.js
82 lines (81 loc) · 2.2 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import deepDiff from "deep-diff";
import { detailedDiff } from "deep-object-diff";
import { diffJson } from "diff";
import microdiff from "./dist/index.js";
import { hrtime } from "node:process";
import colors from "picocolors";
const characters = "abcdefghijklmnopqrstuvwxyz1234567890".split("");
async function benchmark(name, obj, newObj, exclude = []) {
const benchmarks = {
"deep-diff": () => deepDiff.diff(obj, newObj),
"deep-object-diff": () => detailedDiff(obj, newObj),
jsdiff: () => diffJson(obj, newObj),
microdiff: () => microdiff(obj, newObj),
};
let times = {};
for (let benchmark in benchmarks) {
if (exclude.includes(benchmark)) {
continue;
}
times[benchmark] = [];
for (let i = 1; i < 10000; i++) {
let time = hrtime();
benchmarks[benchmark]();
times[benchmark].push(hrtime(time)[1]);
}
times[benchmark] =
times[benchmark].reduce((pv, nv) => pv + nv) / times[benchmark].length;
}
let output = [];
let fastest = "";
for (let time in times) {
if (!fastest || times[time] < times[fastest]) {
fastest = time;
}
}
for (let time in times) {
output.push(
`${time}: ${Math.round(times[time])}ns - ${
fastest === time
? colors.bold(colors.green("Fastest"))
: `${Math.round((times[time] / times[fastest] - 1) * 100)}% slower`
}`
);
}
console.log(
colors.bold(colors.green(`Benchmarks: ${name}\n`)) + output.join("\n")
);
}
console.log(colors.bold("Starting Benchmark"));
benchmark(
"Small object (baseline)",
{
name: "Testing",
propertyTwo: "Still testing...",
},
{
name: "TestingChanged",
propertyThree: "Still testing...",
}
);
let largeObj = {};
let i = 0;
while (i < 300) {
let randomString = "";
for (let characterCount = 0; characterCount < 5; characterCount++) {
randomString += characters[Math.round(Math.random() * characters.length)];
}
if (!largeObj[randomString]) {
largeObj[randomString] = Math.random() * 100;
i++;
}
}
let newLargeObj = {};
for (let randomProperty in largeObj) {
if (Math.random() > 0.95) {
newLargeObj[randomProperty] = Math.random() * 100;
} else if (!Math.random() < 0.975) {
newLargeObj[randomProperty] = largeObj[randomProperty];
}
}
benchmark("Large Object (300 properties)", largeObj, newLargeObj);