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

weakbind

v1.0.0

Published

Memoized bind for higher performance.

Downloads

2

Readme

weakbind

Build Status

Memoized bind for higher performance.

Usage

npm install --save weakbind
var weakbind = require("weakbind");

function fn () {
    return this.x;
}

var context = { x: 1 };
var boundFn = weakbind(x);
boundFn() // 1

What is it?

It's a replacement for the native bind method, but with the distinction that it always returns the same function instead. This is especially useful in React where you pass callbacks down the tree and creating a new instance invalidates your shouldComponentUpdate checks.

How Does it Work?

Internally it uses a two-dimensional WeakMap where the first level key is the function and second level key is the context. This means that it doesn't need to leak memory or attach new properties to your functions.

Pitfalls

  • For browsers that don't support WeakMap, it provides only a sham, so for example you can't rely on the result being the same if you have to support older JS runtimes. This is a tradeoff between leaking memory and performance, and leaking memory is never acceptable. However, even for older runtimes it's faster than native bind in most cases.
  • Can't be used as a drop-in replacement for native bind, as the partial application functionality of native bind is not supported.
  • While faster than native bind in most cases, if the functions you're binding are recreated all the time anyway, weakbind will actually be slower than native bind.

Benchmark

The benchmark (benchmark.js) was run on my local machine. You can run it yourself to see if the results differ.

Without WeakMap (node.js v.0.10.33)

normal bind (trashing): 0.0015860912109973602ms (average) 0.001094ms (median)
normal bind (repeating): 0.0015432097600004845ms (average) 0.001103ms (median)
weakbind (trashing): 0.0004950802689996692ms (average) 0.000339ms (median)
weakbind (repeating): 0.0004421687919996935ms (average) 0.000331ms (median)

With WeakMap (iojs v.2.0.2)

normal bind (trashing): 0.0012574215769975928ms (average) 0.00117ms (median)
normal bind (repeating): 0.001258740308001994ms (average) 0.001195ms (median)
weakbind (trashing): 0.0055595191419998595ms (average) 0.001625ms (median)
weakbind (repeating): 0.0010129746209979584ms (average) 0.000705ms (median)