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

funcualizer

v0.0.1

Published

Converting methods to performant functions.

Downloads

3

Readme

Funcualizer

Small Javascript node library for converting methods into performant functions.

const funcualizer = require('funcualizer');

const slice = funcualizer(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice("abc", 0, 2) === ['a', 'b']

Functionizing a method allows it to be used and passed around as a true function, without having to .call it.

The library provides helpers to create functions that take this as either an explicit first or last parameter:

const post_slice = funcualizer.post(Array.prototype.slice);

post_slice(2, [1, 2, 3]) === [3]
post_slice(0, 2, "abc") === ['a', 'b']

The library allows you to choose between more generic behavior and a more performant implementation, with some of the resulting functions having overhead comparable to a handwritten method-to-function implementation.

Documentation

funcualizer(method)

funcualizer.pre(method)

Convert method into a function that takes this as a first argument. Forwards all other arguments to method.

const slice = funcualizer.pre(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice([1, 2, 3], 0, 2) === [1, 2]

This is usually the slowest implementation. Try using pre$ for better performance if perfect forwarding is not needed.

pre is also the top level export of the package:

funcualizer(Array.prototype.slice) === funcualizer.pre(Array.prototype.slice)

funcualizer.pre$(method [, arity])

Same as pre but for methods with a fixed number of arguments.

arity is the number of arguments that method expects. If arity is not provided, it is inferred from method.length.

// infers `arity === 2` from `Array.prototype.slice.length`.
const slice = funcualizer.pre$(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice([1, 2, 3], 0, 2) === [1, 2]

This is usually much faster than pre, but cannot forward arbitrary argument sets like pre can.

funcualizer.dynamic_pre(methodName)

Same general behavior as pre but looks up a method by name on the this argument. This means that the actual method implementation is not known until the function is invoked:

const toString = funcualizer.dynamic_pre('toString');

toString([1, 2, 3]) === "1,2,3"
toString({}) === "[object Object]"
toString({ toString: () => "bla" }) === 'bla'

funcualizer.dynamic_pre$(methodName, arity)

Fixed argument version of dynamic_pre.

Unlike pre$, since there is no method object to worth with here, the arity must be provided in order to gain any performance over regular dynamic_pre.


funcualizer.post(method)

Convert method into a function that takes this as a last argument. Forwards all other arguments to method.

const slice_post = funcualizer.post(Array.prototype.slice);

slice_post(2, [1, 2, 3]) === [3]
slice_post(0, 2, "abc") === ['a', 'b']

This is usually the slowest implementation. Try using post$ for better performance if perfect forwarding is not needed.

funcualizer.post$(method [, arity])

Same as post but for methods with a fixed number of arguments.

arity is the number of arguments that method expects.

// infers `arity === 2` from `Array.prototype.slice.length`.
const slice_post = funcualizer.post$(Array.prototype.slice);

slice_post(2, [1, 2, 3]) === [3]
slice_post(0, 2, "abc") === ['a', 'b']

Unlike pre$ however, the this argument is always at index arity, even if you invoke the function with less than the number of expected arguments.

This is usually much faster than post, but cannot forward arbitrary argument sets like post can.

funcualizer.dynamic_post(methodName)

Same general behavior as post but looks up a method by name on the this argument. See dynamic_pre.

funcualizer.dynamic_post$(methodName, arity)

Fixed argument version of dynamic_post.