npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

memor

v0.2.0

Published

More memoization

Downloads

9

Readme

Memor: More memoization.

npm version

Memoization, but good. Works with functions of an arbitrary and/or variable number of arguments. For arrays, regexes, dates, buffers, and POJOs, caching is done according to the value (and not the identity) of the objects. Order of keys in POJOs does not matter. For other non-primitive values, memoization still works, but the caching is done by object identity.

Requirements

Usage

memor.memoize

import { memoize } from 'memor'

const memoizedFunction = memoize(originalFunction)

memoizedFunction(/* ... */)

originalFunction can accept any number or a variable number of arguments. Re-memoizing the same function (i.e., calling memoize(originalFunction) elsewhere later) will share the cached values.

Keying of primitives, regexes, dates, and buffers works according to their values. Any additional custom properties added to the objects will not be considered as part of the key. More specifically, regexes and buffers are keyed according to their .toString(), and dates are keyed according to their .getTime().

Keying of arrays (prototype Array.prototype), POJOs (prototype Object.prototype), and prototype-less objects (prototype null) works according to their enumerable and non-enumerable property names and symbols and their values, without regard to the order they appear.

Other objects are by default simply keyed according to their identity (i.e., ===), although this can be extended (see memor.add, below).

Take a look at the unit tests in test.js for some specific examples of what will and will not get keyed the same way.

memor.clear

import { memoize, clear } from 'memor'

const memoizedFunction = memoize(originalFunction)

memoizedFunction(/* ... */)

clear(originalFunction)
// or
clear(memoizedFunction)

memoizedFunction(/* ... */)

All memoized values for a function can be cleared by calling clear on the original function or on the memoized function. These do exactly the same thing: Since all memoized copies of the same function share the same cache, clearing one clears all of them.

memor.add

import { add } from 'memor'

add(Class1, Class2, ...)

This makes keying of instances of Class1, Class2, etc. work the same as arrays, POJOs, and prototype-less objects. That is, keyed according to their prototype and their enumerable and non-enumerable property names and symbols and their values, without regard to the order they appear. Note that this only sets up handling for direct instances of Class1, etc. (i.e., those objects whose prototype is Class1.prototype, etc.).

memor.addCustom

import { addCustom } from 'memor'

addCustom(Class1, Class2, ..., (obj, push, recurse) => { /* ... */ })

This allows more customization of how instances of Class1, Class2, etc. are keyed. In general, keying objects involves converting them into a linear array of primitives and objects to use as Map/WeakMap keys. Two objects will be keyed together if and only if they are converted to arrays of the same (===) primitives and objects.

A custom handler is implemented by writing a function that accepts three arguments: obj (the object to compute the representation for), push (a function to be called with one or more primitives or objects to append to the representation for this object), and recurse (a function to be called to insert the representation for another object). Take a look at the implementations of the existing handlers in handlers.js for more details on how these could work.

memoizedFunction.original

import { memoize } from 'memor'

const memoizedFunction = memoize(originalFunction)

memoizedFunction.original === originalFunction // true

The original function is available as the .original property on the memoized function.

Misc

License

Copyright (c) 2018 Conduitry