-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark-v2-mmm-yyyy.js
65 lines (57 loc) · 1.4 KB
/
benchmark-v2-mmm-yyyy.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
import Benchmark from "benchmark";
import { format as dateFnsFpFormat } from "date-fns/fp/index.js";
import DateFormatter from "fast-date-format";
import speedDate from "speed-date";
const dateFormatter = new DateFormatter("MMM_YYYY");
const speedDateFormatter = speedDate("MMM_YYYY");
const d = new Date();
const monthLookup = {
0: "Jan",
1: "Feb",
2: "Mar",
3: "Apr",
4: "May",
5: "Jun",
6: "Jul",
7: "Aug",
8: "Sep",
9: "Oct",
10: "Nov",
11: "Dec",
};
const benchmarks = {
"fast-date-format": (date) => dateFormatter.format(date),
"speed-date": (date) => speedDateFormatter(date),
"date-fns/fp": (date) => dateFnsFpFormat("MMM_yyyy", date),
custom: (date) => {
const month = date.getMonth();
const year = date.getFullYear();
return `${monthLookup[month]}_${year}`;
},
};
const now = new Date();
console.table(
Object.fromEntries(
Object.entries(benchmarks).map(([name, fn]) => [name, fn(now)])
)
);
Object.entries(benchmarks)
.reduce(
(suite, [name, fn]) =>
suite.add(name, () => {
fn(new Date());
}),
new Benchmark.Suite("Date Format", {})
)
.on("cycle", function (event) {
console.log(String(event.target));
})
.on("complete", function () {
console.log("");
console.log(
`🏁 🏁 🏁 '${this.filter("fastest").map(
"name"
)}' is the fastest. 🏁 🏁 🏁`
);
})
.run({ async: true });