Open rol-jacob opened 1 year ago
Luxon uses caching in various places to speed up slow operations, like computing the week information or (more importantly) Intl
operations (which are often notoriously slow).
In theory we could avoid using the DateTime
object for this cache and instead use a WeakMap
and make DateTime
truly immutable. I am not sure on the browser support for WeakMap
though.
I am not sure on the browser support for
WeakMap
though.
It's quite good: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap#browser_compatibility
Yep, 97%, even ie11 supports it: https://caniuse.com/mdn-javascript_builtins_weakmap
I looked into it and unfortunately using WeakMap
is only about half as fast as a plain property on the DateTime object.
You can see my code here: https://github.com/moment/luxon/compare/master...diesieben07:luxon:issue/1518/caching-improvements
Benchmarking results:
DateTime#weekday x 168,758,367 ops/sec ±1.71% (89 runs sampled)
DateTime#weekday old cache x 306,887,551 ops/sec ±0.40% (97 runs sampled)
DateTime#weekday no cache x 3,159,972 ops/sec ±0.35% (99 runs sampled)
As you can see from the "no cache" entry, not using a cache here is not really viable. Any other suggestions are welcome.
I'm working on a Vue app myself that makes heavy use of weekNumber
and co, and have found that they're surprisingly slow - I'm just wondering if there is anything blocking using WeakMap
since it sounded like it was well enough supported and did give a significant performance boost even if it's not as fast as plain properties?
The thing blocking the use of WeakMap
is that it is twice as slow as the current option.
Describe the bug When using luxon in frontend libraries like Vue.js and setting a luxon object as a reactive variable. Reading e.g. .weekNumber from the luxon object will trigger watchers. To Reproduce Please share a minimal code example that triggers the problem:
Actual vs Expected behavior Reading a number should not write to object model if not necessary.
Desktop (please complete the following information):