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 🙏

© 2025 – Pkg Stats / Ryan Hefner

memoize-bind

v1.0.3

Published

Memoized function binding

Downloads

26,450

Readme

memoize-bind

npm version Stability Build Status

Memoized function binding

memoize-bind performs the same job as Function.prototype.bind(), however it memoizes the result for future reference.

This ensures that calling memoize-bind repeatedly with the same arguments will always return the same bound function, which can be particularly useful within React.js render methods (see below).

Installation

npm install memoize-bind

Usage

bind(fn, [context], [...args])

Create a new function that, when called, will invoke fn with the this keyword set to context.

If any args are specified, they will be prepended to the list of arguments supplied when calling the newly-created function.

Example

import bind from 'memoize-bind';
import assert from 'assert';

const context = { name: 'foo' };
const fn = function(...args) { return [this.name, ...args]; };
const args = ['bar', 'baz']

const boundFn = bind(fn, context, ...args); // Identical to fn.bind(context, ...args)
const boundFn2 = bind(fn, context, ...args); // Returns the same function
assert(boundFn === boundFn2);

boundFn('qux'); // Returns ['foo', 'bar', 'baz', 'qux']

Why memoize-bind?

Function.prototype.bind() is often used within React components, where callbacks need to be bound to the current component instance (optionally with arguments pre-applied).

While convenient, using .bind() within a render function can be problematic for React's dirty-checking algorithm, seeing as a new function instance would be returned on each render, leaving the React reconciler unable to tell that two functions generated during subsequent renders are indeed identical.

memoize-bind fixes this problem by always returning the same bound function when called with the same arguments, allowing bound functions to be used within straightforward equality checks in the component's shouldComponentUpdate method (as used by PureComponent / PureRenderMixin).

This means that you can keep all the convenience of the .bind() method, without having to worry about affecting performance.

React Example

import bind from 'memoize-bind';
import React, { PropTypes, PureComponent } from 'react';

class TodoList extends PureComponent {
  render() {
    const { items } = this.props;
    return (
      <ul>
        {items.map((item, index) => (
          <li onClick={bind(this.handleItemClicked, this, item)} key={item.id}>{item.label}</li>
        ))}
      </ul>
    );
  }

  handleItemClicked(item) {
    if (!item.isCompleted) { this.props.onComplete(item); }
  }

  static propTypes = {
    items: PropTypes.arrayOf(PropTypes.shape({
      id: PropTypes.number,
      label: PropTypes.string,
      isCompleted: PropTypes.bool,
    })),
    onComplete: PropTypes.func.isRequired,
  };
}

Using memoize-bind in ES5 applications

memoize-bind uses memoize-weak internally to ensure that any memoized arguments and values can be properly garbage-collected.

memoize-weak requires that Map and WeakMap are globally available. This means that these will have to be polyfilled for use in an ES5 environment.

Some examples of Map and WeakMap polyfills for ES5: