-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark-v2-q-yyyy.js
45 lines (40 loc) · 1.09 KB
/
benchmark-v2-q-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
import Benchmark from "benchmark";
import { format as dateFnsFpFormat } from "date-fns/fp/index.js";
import speedDate from "speed-date";
const speedDateFormatter = speedDate("Q_YYYY");
const benchmarks = {
"speed-date": (date) => speedDateFormatter(date),
"date-fns/fp": (date) => dateFnsFpFormat("Q_yyyy", date),
custom: (date) => {
const month = date.getMonth();
const year = date.getFullYear();
const quarter = Math.floor(month / 3) + 1;
return `${quarter}_${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 });