iamkun / iamkun/dayjs

Very low performance of comparison functions

Open
#2,344 2 comments 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Hi! I'm doing migration from moment.js to dayjs. I have some heavy charts with a lot of comparing dates functionalities. I made a simple benchmark to compare moment and dayjs performance on specific functions.

This is the simple function i was using to test it I just changed the methods inside.
```
const benchmark = () => {
console.time("dayjs");
const date1 = dayjs("2018-04-13 19:18");
const date2 = dayjs("2020-04-13 19:18");
for (let i = 0; i < 10000; i++) {
date1.isSameOrBefore(date2);
date1.isSameOrAfter(date2);
}
console.timeEnd("dayjs");

console.time("moment");
const date3 = moment("2018-04-13 19:18");
const date4 = moment("2020-04-13 19:18");
for (let i = 0; i < 10000; i++) {
date3.isSameOrBefore(date4);
date3.isSameOrAfter(date4);
}
console.timeEnd("moment");
};
```
.isSameOrBefore() / .isSameOrAfter()

![image](https://github.com/iamkun/dayjs/assets/62953138/a59d68e7-f7b9-420f-b56e-c120838406aa) ![image](https://github.com/iamkun/dayjs/assets/62953138/5f3344af-29c4-4de3-b39d-b768eb0ad813) ![image](https://github.com/iamkun/dayjs/assets/62953138/d413de98-821d-4fd8-aff9-dfb6f1797215)

.startOf('month) / .endOf('month)

![image](https://github.com/iamkun/dayjs/assets/62953138/1ba9cba8-ec30-46ed-896a-02108d64f1af) ![image](https://github.com/iamkun/dayjs/assets/62953138/f4901480-bd93-413d-8b14-8b619f68c65f) ![image](https://github.com/iamkun/dayjs/assets/62953138/cf55094e-169d-494d-a844-69a0a37570c1)

.isBefore() / .isAfter()

![image](https://github.com/iamkun/dayjs/assets/62953138/82344bf7-4738-4ac7-8fc1-12e0b53a006f)

.isSame()

![image](https://github.com/iamkun/dayjs/assets/62953138/3344caef-2a63-4c35-a7eb-9ce975ba68c7)

.isBetween()

![image](https://github.com/iamkun/dayjs/assets/62953138/19f2f729-32f0-4506-995e-ef2144a5cf16)

I hope u guys gonna help me maybe I'm doing something wrong with these functions.

As you can see these differences have huge impact on my usage of this library.
My dayjs with plugins instance (I checked without plugins (isBefore,isAfter,isSame) and it has the same results

```
// Load dayjs, plugins and language packs.
import dayjs from 'dayjs';
import timeZonePlugin from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
import duration from 'dayjs/plugin/duration';
import isBetween from 'dayjs/plugin/isBetween';
import isoWeek from 'dayjs/plugin/isoWeek';
// Register plugins and language packs;.
dayjs.extend(timeZonePlugin);
dayjs.extend(utc);
dayjs.extend(customParseFormat);
dayjs.extend(isSameOrBefore);
dayjs.extend(isSameOrAfter);
dayjs.extend(duration);
dayjs.extend(isBetween);
dayjs.extend(isoWeek);

export default dayjs;

```
**Information**
- "moment": "2.29.3", "dayjs": "1.11.8",
- Win 11
- Chrome newest
- Time zone: GMT+2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.