Giter Club home page Giter Club logo

Comments (6)

anywhichway avatar anywhichway commented on July 4, 2024 2

from nano-memoize.

igalklebanov avatar igalklebanov commented on July 4, 2024 1

For single object arguments, when not providing a serializer, the lib defaults to using a weakmap.
Weakmaps compare keys by reference.

You're passing a different object reference in the calls you make to your memoized function.
So the internal weakmap does not have an entry for the object in the second call.

For single argument function such as yours, either:

  1. Call your function as follows:
const obj = { foo: 'foo', bar: fib };
mfn(obj); // miss
mfn(obj); // hit
  1. Provide a serializer function.

If a serializer function is provided, and no equals function is provided, the lib will use the serializer function's returned value as a key in the cache (which is a plain object).

So the serializer must return values that can be keys in plain javascript objects - strings or numbers.

The most common is stringifying objects.

obj => JSON.stringify(obj)

... or other string representations that suit you.

If your objects have unique identifier/s and your function only cares about the identity of the objects, I'd return just the identifier/s.

from nano-memoize.

richardscarrott avatar richardscarrott commented on July 4, 2024

Make sense, thanks for the info!

from nano-memoize.

anywhichway avatar anywhichway commented on July 4, 2024

@igalklebanov thanks for jumping in with a response

from nano-memoize.

anywhichway avatar anywhichway commented on July 4, 2024

@richardscarrott JSON.stringify is slow. Suggest you try to avoid it.

from nano-memoize.

richardscarrott avatar richardscarrott commented on July 4, 2024

@anywhichway yeah I've found it to be slow. Ultimately I'd like fn({ a, b, c }) and fn(a, b, c) to be treated in the same way; i.e. I'd like a, b and c to be compared by identity.

const fib = new Fib(); // assume Fib cannot be serialized w/ `JSON.stringify` or `toString`

// This works by default
mfn('foo', 'bar', fib) // miss
mfn('foo', 'bar', fib) // hit
mfn('foo', 'bar', new Fib()) // miss

// But this is obv a miss every time
mfn2({ a: 'foo', b: 'bar', c: fib }) // miss
mfn2({ a: 'foo', b: 'bar', c: fib }) // hit
mfn2({ a: 'foo', b: 'bar', c: new Fib() }) // miss

How does nano-memoize serialize fn(a, b, c) under the hood?

from nano-memoize.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.