You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Date so slow that it can be freeze app. Impossible to use third party date library(much worse perf, 1800x).
I found that local date is a problem. UTC date still slow but not 800x, just 8x slower than JSC which can be ignored.
The code outputs:
JSC: 0.2605330003425479
Hermes: 166.44708999991417
Hermes UTC: 1.6460870001465082
I have run gradle clean and confirmed this bug does not occur with JSC
Hermes version: Bundled version with RN 0.71.3
React Native version (if any): 0.71.3
OS version (if any): iPhone 14 (simulator)
Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64):
Steps To Reproduce
Create initial app with latest RN version.
Add somewhere the code that down below. Look console to see the numbers.
code example:
vara=performance.now();Array(1000).fill(0).map(()=>{newDate(newDate().setMonth(1));// fast utc version // new Date(new Date().setUTCMonth(1));});varb=performance.now();console.log(b-a);
The Expected Behavior
Fast Date operations.
Reacted by Ragnar Rebase, Tom McIntosh, Denys Pavlenko, Kyun Heo, Wojciech Król, javascripter, Lukas, Ian Park, cheolho, James Holcomb and 25 more
Thank you for reporting this. It looks like an actual problem and is likely caused by us not caching the timezone information.
As an experiment, we changed the system timezone while the JS process was running to see whether it would invalidate the timezone. Hermes displayed the correct new timezone, while JSC and v8 displayed the original one. It is not entirely clear which behavior is preferable.
Could you try a similar experiment in your mobile app?
I can confirm this, I previously used spacetime js to simplify date comparisons in my expo managed app. With the newest release of expo 48 we transitioned to hermes engine and had to remove all spactime js uses because just 150 items slowed down the app to a standstill for several seconds whilst running date comparisons.
This is now our code:
And this is also slower than it was using jsc, by a large margin.
We agree that this needs to be fixed to match the behavior of the other engines. Unfortunately we don't have concrete plans on when exactly we can work on the fix yet. Meanwhile a community contribution would be welcome.
Thank you for the quick response @tmikov!
I just wonder how people handle it in their projects at the moment (don't use Hermes?). It's quite hard to find any opened issues or articles about the problem but I think almost every project has some interactions with dates
Thank you for the quick response @tmikov!
I just wonder how people handle it in their projects at the moment (don't use Hermes?). It's quite hard to find any opened issues or articles about the problem but I think almost every project has some interactions with dates
The "solution" in our case is indeed not using Hermes atm.
@tmikov The simplest most straight forward solution here would be to expose an API from hermes which will cache the timezone info on demand instead of doing it automatically on every call.
@rpopovici are there APIs for updating the cached timezone information in JSC? In other words, if a RN app is running in JSC and a system timezone change occurs, how can you force it to refresh its cache? If there is no such API, how can the app handle timezone changes at all?
@tmikov if relying on timezone change events is a deviation from the standard behaviour, then trying to debounce the timezone info every second could be an acceptable compromise solution which could alleviate the perf bottleneck and is better than JSC or v8 since these two won't update the timezone data at all.
Also the timezone debouncing solution can be easily exposed through a hermes runtime config flag and then you can keep both behaviours and let the user decide if they need super accurate timezone info or they can settle for something less accurate but more performant
Reacted by Tzvetan Mikov, javascripter, Gary Tokman , Conrad and Fedir Ushakov
@TiStyle I refactored all of my code to avoid Date instances (except for display in the UI). I just use plain numbers and do simple math when I need to add/subtract/calculate things. Use Date.now() to get a number instead of a Date.
@evelant I was hoping to avoid such things, but it seems there is no other solution but for now(). Haha, get it?.. Thank you for your advice, I'll just have to start this painstakingly task.
Reacted by vomchik, Serhii Tonkoshkur, Roman D, Tom McIntosh, Victor Kozodaev and Denys Pavlenko
I wanted to share that we are also facing this issue in our React Native app.
We have an infinite list of messages, each of which render a date, which must be calculated by the difference between sendTime and now.
To "solve" this, we can eagerly render the list, then defer the date calculations inside InteractionManager. This greatly sped up the initial paint of the list.
/** * DateTime calculation are currently very slow in Hermes https://github.com/facebook/hermes/issues/930 * This means each MessageTime would add about 20ms to the list item render. * * To 'resolve' this, we start with empty string, then wrap the formatFunction in InteractionManager, * so that it does block the first paint. This tradeoff is that the timestamp loads slightly after the message, * but the message list renders in 1/3rd the time. * * This optimisation can be removed when the above issue is solved. * */exportconstMessageTime=({ timestamp, prefix }: Props)=>{const[text,setText]=useState('');useEffect(()=>{constcalculateStamp=()=>InteractionManager.runAfterInteractions(()=>{setText(formatTimestamp(newDate(),timestamp));});calculateStamp();constinterval=setInterval(()=>{calculateStamp();},oneMinute);return()=>clearInterval(interval);},[timestamp]);return(<Viewstyle={styles.container}><TextmutedfontSize={'sm'}numberOfLines={1}>{prefix}{text}</Text></View>);};conststyles=StyleSheet.create({container: {display: 'flex',flexDirection: 'row',flexShrink: 1,},});
In our case DayJS was the problem, so I made some quick performance improvments and it now runs 1000x faster on iOS simulator:
NOTE: See @km-tr 's comment below to address using dayjs().set(...) as my solution does not account for mutability.
diff--gita/node_modules/dayjs/esm/index.jsb/node_modules/dayjs/esm/index.jsindexa82986b..1fb8898100644---a/node_modules/dayjs/esm/index.js+++b/node_modules/dayjs/esm/index.js
@@ -117,17+117,101 @@ varDayjs=/*#__PURE__*/function(){_proto.init=functioninit(){var$d=this.$d;-this.$y=$d.getFullYear();-this.$M=$d.getMonth();-this.$D=$d.getDate();-this.$W=$d.getDay();-this.$H=$d.getHours();-this.$m=$d.getMinutes();-this.$s=$d.getSeconds();-this.$ms=$d.getMilliseconds();+// this.$y = $d.getFullYear();+// this.$M = $d.getMonth();+// this.$D = $d.getDate();+// this.$W = $d.getDay();+// this.$H = $d.getHours();+// this.$m = $d.getMinutes();+// this.$s = $d.getSeconds();+// this.$ms = $d.getMilliseconds();}// eslint-disable-next-line class-methods-use-this;+// Avoid running native Date functions like getFullYear until needed because these+// functions are super slow in Hermes. Then run once and cache since Dayjs is immutable.++Object.defineProperty(Dayjs.prototype,'$y',{+get(){+if(!this.cached_y){+this.cached_y=this.$d.getFullYear();+}+returnthis.cached_y;+}+});++Object.defineProperty(Dayjs.prototype,'$M',{+get(){+if(!this.cached_M){+this.cached_M=this.$d.getMonth();+}+returnthis.cached_M;+}+});++Object.defineProperty(Dayjs.prototype,'$D',{+get(){+if(!this.cached_D){+this.cached_D=this.$d.getDate();+}+returnthis.cached_D;+}+});++Object.defineProperty(Dayjs.prototype,'$W',{+get(){+if(!this.cached_W){+this.cached_W=this.$d.getDay();+}+returnthis.cached_W;+}+});++Object.defineProperty(Dayjs.prototype,'$H',{+get(){+if(!this.cached_H){+this.cached_H=this.$d.getHours();+}+returnthis.cached_H;+}+});++Object.defineProperty(Dayjs.prototype,'$m',{+get(){+if(!this.cached_m){+this.cached_m=this.$d.getMinutes();+}+returnthis.cached_m;+}+});++Object.defineProperty(Dayjs.prototype,'$s',{+get(){+if(!this.cached_s){+this.cached_s=this.$d.getSeconds();+}+returnthis.cached_s;+}+});++Object.defineProperty(Dayjs.prototype,'$ms',{+get(){+if(!this.cached_ms){+this.cached_ms=this.$d.getMilliseconds();+}+returnthis.cached_ms;+}+});++Object.defineProperty(Dayjs.prototype,'$timezoneOffset',{+get(){+if(!this.cached_timezoneOffset){+this.cached_timezoneOffset=this.$d.getTimezoneOffset();+}+returnthis.cached_timezoneOffset+}+});+_proto.$utils=function$utils(){returnUtils;};
@@ -137,15+221,27 @@ varDayjs=/*#__PURE__*/function(){};_proto.isSame=functionisSame(that,units){+if(units===undefined){+returnthis.toISOString()===dayjs(that).toISOString()+}+varother=dayjs(that);returnthis.startOf(units)<=other&&other<=this.endOf(units);};_proto.isAfter=functionisAfter(that,units){+if(units===undefined){+returnthis.toISOString()>dayjs(that).toISOString()+}+returndayjs(that)<this.startOf(units);};_proto.isBefore=functionisBefore(that,units){+if(units===undefined){+returnthis.toISOString()<dayjs(that).toISOString()+}+returnthis.endOf(units)<dayjs(that);};
@@ -408,7+504,7 @@ varDayjs=/*#__PURE__*/function(){_proto.utcOffset=functionutcOffset(){// Because a bug at FF24, we're rounding the timezone offset around 15 minutes// https://github.com/moment/moment/pull/1871-return-Math.round(this.$d.getTimezoneOffset()/15)*15;+return-Math.round(this.$timezoneOffset/15)*15;};_proto.diff=functiondiff(input,units,_float){
Reacted by Stathis Ntonas, Yuta Kumagai and Sherlock Luk
I noticed 150ms increase in render time with dayjs, without the date the component rendered in 50ms, converted to Date and it increased to 70ms which is way faster than the 200ms with dayjs.
@gaberogan
I tried your patch, but I encountered an issue where executing something like dayjs(date).day(0) causes the date to shift. Have you addressed this problem?
For example, today is the 22nd, and when I use day(0), I expect it to return the 19th, but it returns the 22nd instead.
However, when I use add(1), it correctly returns the 20th.
@km-tr I haven't seen this problem, but thanks for mentioning it. If it's a problem with my diff and you find a solution, I'll update the diff so others don't run into the same problem.
Thank you for checking.
After trying various solutions myself, I found that initializing the cache within init resolved the issue.
It seems that init is executed at the end of $set within dayjs, and if the cache isn’t initialized, the cached value is used instead of the value set by $set.
Since importing dayjs/esm caused other issues, I forked the repository to address them.
For now, I’m installing from GitHub, so I have included the build files in the commit.
We have made some improvements to localTime()/utcTime() performance in Hermes. On a simple benchmark with pure Date usage in a loop, it should have performance comparable to V8/JSC. On the dayjs benchmark above, it's ~2X faster than before (still ~2X slower than V8 but the main bottleneck is not very relevant to Date and Time computations now).
Reacted by Manu, vomchik, Simon Trény, Axel Elmarsson, Yuta Kumagai, Ragnar Rebase, Tom McIntosh and rohovdmytro
Reacted by Lukas May, Simon Trény, Pearce Keesling, Liam Jones, Yuta Kumagai, Ragnar Rebase and rohovdmytro
Reacted by Michael Wood, Lukas May, Simon Trény, Gabriel Rogan, Yuta Kumagai, Ragnar Rebase and rohovdmytro
HI there, it has been a few months now and we're very eager to find a solution to this.
We're using Luxon, as well as normal Date objects. Our applications has hundreds of timers and countdowns, so it's having a massive effect on performance. It's not realistic to re-write everything without Date(). Is there any update on when this will merge?
Activity
tmikov commentedon Mar 4, 2023
Thank you for reporting this. It looks like an actual problem and is likely caused by us not caching the timezone information.
As an experiment, we changed the system timezone while the JS process was running to see whether it would invalidate the timezone. Hermes displayed the correct new timezone, while JSC and v8 displayed the original one. It is not entirely clear which behavior is preferable.
Could you try a similar experiment in your mobile app?
ljukas commentedon Mar 6, 2023
I can confirm this, I previously used spacetime js to simplify date comparisons in my expo managed app. With the newest release of expo 48 we transitioned to hermes engine and had to remove all spactime js uses because just 150 items slowed down the app to a standstill for several seconds whilst running date comparisons.
This is now our code:
And this is also slower than it was using jsc, by a large margin.
rpopovici commentedon Mar 6, 2023
@tmikov this is a major problem for anything which relies on dates to generate unique identifiers or localized translations..
Both iOS and android have mechanisms to notify the app when there is a timezone change.
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622992-applicationsignificanttimechange
https://developer.android.com/reference/android/content/Intent#ACTION_TIMEZONE_CHANGED
IMO, caching the timeszone info and then updating that info only when there is an actual timezone change could be a potential solution here.
DePavlenko commentedon Apr 27, 2023
We have exactly the same issue. Because of it we cannot switch to Hermes engine unfortunately.
I run this function in a sorting loop (~500 items in a list)
Here is a preview:
And then I tried to use isSame function form dayjs and it took ~15500ms!!! to complete sorting
tmikov commentedon Apr 27, 2023
We agree that this needs to be fixed to match the behavior of the other engines. Unfortunately we don't have concrete plans on when exactly we can work on the fix yet. Meanwhile a community contribution would be welcome.
DePavlenko commentedon Apr 28, 2023
Thank you for the quick response @tmikov!
I just wonder how people handle it in their projects at the moment (don't use Hermes?). It's quite hard to find any opened issues or articles about the problem but I think almost every project has some interactions with dates
harrigee commentedon Apr 28, 2023
The "solution" in our case is indeed not using Hermes atm.
rpopovici commentedon Apr 28, 2023
@tmikov The simplest most straight forward solution here would be to expose an API from
hermeswhich will cache the timezone info on demand instead of doing it automatically on every call.tmikov commentedon May 18, 2023
@rpopovici are there APIs for updating the cached timezone information in JSC? In other words, if a RN app is running in JSC and a system timezone change occurs, how can you force it to refresh its cache? If there is no such API, how can the app handle timezone changes at all?
rpopovici commentedon May 18, 2023
@tmikov if relying on timezone change events is a deviation from the standard behaviour, then trying to debounce the timezone info every second could be an acceptable compromise solution which could alleviate the perf bottleneck and is better than JSC or v8 since these two won't update the timezone data at all.
Also the timezone debouncing solution can be easily exposed through a hermes runtime config flag and then you can keep both behaviours and let the user decide if they need super accurate timezone info or they can settle for something less accurate but more performant
43 remaining items
evelant commentedon Dec 8, 2023
@TiStyle I refactored all of my code to avoid Date instances (except for display in the UI). I just use plain numbers and do simple math when I need to add/subtract/calculate things. Use
Date.now()to get a number instead of a Date.TiStyle commentedon Dec 11, 2023
@evelant I was hoping to avoid such things, but it seems there is no other solution but for now(). Haha, get it?.. Thank you for your advice, I'll just have to start this painstakingly task.
tj-mc commentedon Mar 26, 2024
I wanted to share that we are also facing this issue in our React Native app.
We have an infinite list of messages, each of which render a date, which must be calculated by the difference between sendTime and now.
To "solve" this, we can eagerly render the list, then defer the date calculations inside InteractionManager. This greatly sped up the initial paint of the list.
gaberogan commentedon Mar 29, 2024
In our case DayJS was the problem, so I made some quick performance improvments and it now runs 1000x faster on iOS simulator:
NOTE: See @km-tr 's comment below to address using dayjs().set(...) as my solution does not account for mutability.
efstathiosntonas commentedon Mar 29, 2024
I noticed 150ms increase in render time with dayjs, without the date the component rendered in 50ms, converted to Date and it increased to 70ms which is way faster than the 200ms with dayjs.
km-tr commentedon May 21, 2024
@gaberogan
I tried your patch, but I encountered an issue where executing something like
dayjs(date).day(0)causes the date to shift. Have you addressed this problem?For example, today is the 22nd, and when I use
day(0), I expect it to return the 19th, but it returns the 22nd instead.However, when I use
add(1), it correctly returns the 20th.gaberogan commentedon May 22, 2024
@km-tr I haven't seen this problem, but thanks for mentioning it. If it's a problem with my diff and you find a solution, I'll update the diff so others don't run into the same problem.
km-tr commentedon May 22, 2024
@gaberogan
Thank you for checking.
After trying various solutions myself, I found that initializing the cache within init resolved the issue.
It seems that init is executed at the end of $set within dayjs, and if the cache isn’t initialized, the cached value is used instead of the value set by $set.
km-tr commentedon May 23, 2024
@gaberogan
https://github.com/km-tr/dayjs
Since importing
dayjs/esmcaused other issues, I forked the repository to address them.For now, I’m installing from GitHub, so I have included the build files in the commit.
lavenzg commentedon Jun 26, 2024
We have made some improvements to localTime()/utcTime() performance in Hermes. On a simple benchmark with pure
Dateusage in a loop, it should have performance comparable to V8/JSC. On the dayjs benchmark above, it's ~2X faster than before (still ~2X slower than V8 but the main bottleneck is not very relevant to Date and Time computations now).vomchik commentedon Jun 27, 2024
@lavenzg Thank you! Do you know when we can expect this fix in the RN?
tj-mc commentedon Sep 18, 2024
HI there, it has been a few months now and we're very eager to find a solution to this.
We're using Luxon, as well as normal Date objects. Our applications has hundreds of timers and countdowns, so it's having a massive effect on performance. It's not realistic to re-write everything without Date(). Is there any update on when this will merge?
neildhar commentedon Sep 18, 2024
The performance improvements were made in 59ea64c, and should be part of RN 0.76.
RohovDmytro commentedon Mar 9, 2025
Can somebody confirm that their performance issues related to manipulations of lots of Dates objects got resolved in 0.77.*+?
tj-mc commentedon Apr 22, 2025
In our application @ .77, there is huge improvement and we removed the workaround. Thanks RN Team.