Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: performance issue in tz plugin #2019

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open

Fix: performance issue in tz plugin #2019

wants to merge 2 commits into from

Conversation

logeyG
Copy link

@logeyG logeyG commented Aug 8, 2022

This PR is an adaptation of the following closed PR: #1889

Fixes performance issue in timezone plugin by using a cached Intl.DateTimeFormat formatter instead of toLocaleString(). No functional changes, just an order of magnitudes faster.

The only change I made was to create a default config object that is used for both getDateTimeFormat as well as getLocaleStringifier.

Fixes #1236

@logeyG logeyG marked this pull request as ready for review August 8, 2022 13:27
@defghy
Copy link

defghy commented Aug 10, 2022

image

It seems this 2 function not generate same format;

In my case, getLocaleStringifier worked fine, use getDateTimeFormat make date not correct

@logeyG
Copy link
Author

logeyG commented Aug 10, 2022

Hi @defghy, yes you are correct, but I don't think this is an issue. The dates are the same, just in different formats. The previous code that getLocaleStringifier is replacing had the exact same format when using toLocaleString instead (see line 115):

let t = new Date().toLocaleString('en-US', { timeZone: 'Europe/Paris' })
'8/10/2022, 10:07:10 AM'

Copy link

@tevin tevin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, great fix for the perf issue

@defghy
Copy link

defghy commented Aug 11, 2022

Has some problem when date is Invalid Date;

image

In this situation, an Error throw;
Maybe need some try/catch to remain same as before

@logeyG

@logeyG
Copy link
Author

logeyG commented Aug 12, 2022

@defghy this actually seems to be an improvement, in my opinion.

I think it is OK to throw an error if anything other than a date object is passed in. I can't think of an instance where catching the error is the preferred behavior here.

@logeyG
Copy link
Author

logeyG commented Aug 23, 2022

@iamkun can you please review this?

1 similar comment
@logeyG
Copy link
Author

logeyG commented Sep 19, 2022

@iamkun can you please review this?

@Prabhakar-Poudel
Copy link

@BePo65 @iamkun any chance of getting this reviewed?

@kamrankhan54
Copy link

Anyone know when this could be merged please? We are blocked from using DayJS due to the performance issue mentioned here. @logeyG @iamkun

@Klaitos
Copy link

Klaitos commented Mar 16, 2023

up 👍

@Serg-Mois
Copy link

up up up)

@tgensol
Copy link

tgensol commented Apr 6, 2023

up up ?

@tduyng
Copy link

tduyng commented Apr 14, 2023

Why haven't there been any updates for this issue?

@serg-mois-capital
Copy link

Repo looks like dead, the only author doesn't support it.

@dominikbrazdil
Copy link

up

@kozak-codes
Copy link

Thanks for making such an awesome project @logeyG @iamkun ! I'm hoping to bump this up in your priority list to review as the current state is currently contributing to 2.5% of my nodejs backend application's CPU profile with a wall time of about 162ms. This would be an easy win for improving our applications speed. We've seen tons of performance improvements since switching from moment but this one is definitely an outlier.

Thanks again!

@janousek
Copy link

janousek commented Aug 2, 2023

@logeyG This fix is not correct. Try this:

let date = new Date(2023,9,2,0,0,0);

let val1 = new Intl.DateTimeFormat('en-US', {
		year: 'numeric',
		month: '2-digit',
		day: '2-digit',
		hour: '2-digit',
		minute: '2-digit',
		second: '2-digit',
		hour12: false,
		timeZone: 'Europe/Prague',
    })
	.format(date);

let val2 = date.toLocaleString('en-US', { timeZone: 'Europe/Prague' });


console.log(val1, 'VS', val2);
console.log(new Date(val1), 'VS', new Date(val2));

image

I belive that you have to set hour12 to true.

@keithwillcode
Copy link

keithwillcode commented Feb 2, 2024

@iamkun Bumping this as well. Seeing major performance issues with using the tz plugin as it exists now. The changes proposed in this PR result in orders of magnitude better perf. Happy to help with testing or whatever is needed. Let me know. 🙏

@JorgeHabib
Copy link

Hello guys, firstly I'd like to thank you @iamkun for all for the effort you put into enhancing the dayjs library.

I've also encountered some performance challenges related to the timezone function, which have been impacting my work. I came across PR #2542 and noticed that it addresses these issues effectively, as well as this PR.

Could you share any insights on the timeline for the next release that would include these significant improvements? This update would greatly benefit our projects, and we're looking forward to integrating it.

Thank you for your continued dedication to improving dayjs, we love it.

@adlanarifzr
Copy link

adlanarifzr commented Feb 16, 2024

For temporary measure, you guys can use my forked library for the timezone issue. I have merged this PR.

https://www.npmjs.com/package/@adlanarifzr/dayjs

import timezone from '@adlanarifzr/dayjs/plugin/timezone';
dayjs.extend(timezone);

@JorgeHabib
Copy link

For temporary measure, you guys can use my forked library for the timezone issue. I have merged this PR.

https://www.npmjs.com/package/@adlanarifzr/dayjs

import timezone from '@adlanarifzr/dayjs/plugin/timezone';
dayjs.extend(timezone);

These improvements have significantly enhanced our performance here. Are you/anyone aware of any other functions that might be encountering similar performance issues?

@rushi
Copy link

rushi commented Apr 28, 2024

@iamkun Is it possible for you to review this? It solves a major performance issue with the timezone plugin

ikeyan added a commit to ikeyan/dayjs that referenced this pull request Oct 19, 2024
@ikeyan
Copy link

ikeyan commented Oct 20, 2024

#2753 is an improved version of this PR I made.
I fixed this bug and this breaking change, improved performance even more, and shrunk the gzipped code size.

@ngouy
Copy link

ngouy commented Oct 22, 2024

@defghy this actually seems to be an improvement, in my opinion.

I think it is OK to throw an error if anything other than a date object is passed in. I can't think of an instance where catching the error is the preferred behavior here.

As much as it seem reasonable, it's a breaking change
For example react-big-calendar always start it's state with some null-ish data and start internally to format it. With regular TZ it just silence it and then it's using user param to override state and everyone is happy

With this version, you can try ANY param, state is always initiated with null-ish values and it just crashes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache toLocaleString