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

memobind

v0.5.0

Published

A simple javascript utility for function binding memoization.

Downloads

1,505

Readme

Memobind

Version Build Status MIT licensed

A simple javascript utility for function binding memoization. It's motivated by the requirement of dynamic binding in React component rendering.

Install with npm

npm install memobind

Use with node.js, browserify or webpack:

var memobind = require('memobind');
memobind(context, methodName, ...args);

Motivation

A bind call or arrow function in a JSX prop will create a brand new function on every single render. This is bad for performance, as it will result in the garbage collector being invoked way more than is necessary.

A common use case of bind in render is when rendering a list, to have a separate callback per list item:

<ul>
  {this.props.items.map(item =>
    <li key={item.id} onClick={this.onItemClick.bind(this, item.id)}>
      ...
    </li>
  )}
</ul>

This is not good because it creates new functions in every update. The eslint rule jsx-no-bind is used to detect such quality issues.

To resolve the problem, memobind caches the function bind result so that it could be reused if the arguments are not changed. See below example:

<ul>
  {this.props.items.map(item =>
    <li key={item.id} onClick={memobind(this, 'onItemClick', item.id)}>
      ...
    </li>
  )}
</ul>

How it works

memobind caches the function bind result in the context object, with methodName as the key for cache object, and this[methodName] is the function to bind. So the context object should not be null, it's usually the component itself. The function binding result is stored with the key generated from arguments using JSON.stringify. In the above example, it is JSON.stringify([item.id]).

If you need to call a method on the component props or other objects, wrap it as a component method. For example:

class List extends React.Component {
  onItemClick(itemId) {
    this.props.onItemClick(itemId);
  }

  render() {
    ...
  }
}

memobind is created only for the need of bind with arguments. If there is no arguments, although memobind could be used, autobind decorator is a better choice with ES-future transpilers support such as Babel.

Examples

Simple setState call

<button onClick={memobind(this, 'setState', { popupVisible: true })}>Show Dialog</button>

License

MIT. Copyright (c) 2016 Nate Wang.