planttheidea/moize

Why does moize's performance implode in this test case?

Nantris opened this issue · 2 comments

I forked off some existing tests on MeasureThat.net but was surprised to find that moize totally implodes when the argument is a new object that is equal in value, but not reference identity, to the previous one.

Reference identity - 6.5% faster than memoize-one
Value equality - 50000% slower than memoize-one

Valid question; there are a couple things at play here.

  1. The "value equality" case is only testing "what do these functions do when values change every time?". Because the object references change on every call, nothing is ever memoized.
  2. The reason for the cost is because by default moize versions 5 and below will store all previous values in cache unless a cache size is provided. This means that storage work and existing checks are being done for a constantly-growing cache base, which means an exponential increase in runtime cost and memory.

A more apples-to-apples comparison with memoize-one would not be to use moize but instead moize.simple, which limits the cache size to 1 (which is the cache size for memoize-one). Or you can test with the latest beta, which changes the default cache size from Infinity to 1, again providing a closer apples-to-apples.

In both of these "value equality" cases, though, you are not testing the "memoizing" capabilities of the library, since no memoization is done. Realistically to test the memoization capabilities with value equality, you would need to provide a custom isEqual method to memoize-one and a custom equals method to moize.

Thanks for your reply! Looks like I misread the docs while skimming through. I had read it as maxAge defaults to 1 - overlooking that that was a reference to how moize.simple works.

Thanks also for the pointer about comparing using a custom equals. I'll look into that when I find some more time to allocate to benchmarking stuff.