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

@danielsan/node-lazy-loader

v0.2.0

Published

A Package for NPM Packages! Avoid unnecessary IO and save memory by allowing your users to load only the code they want

Downloads

45

Readme

If you maintain a node package that has multiple functionalities that are not necessarily interdependent you are probably exporting the functionalities on your main js file like this:

module.exports = {
  feature1: require('./lib/feature1'),
  feature2: require('./lib/feature2'),
  feature3: require('./lib/feature3')
 ...
  featureN: require('./lib/featureN')
}

Some developers might install your node packages just because of a single one of those features, and most of them are loading your features like this:

const { feature1 } = require('your-package')
...

Problems

That forces their application to waste: processing time by performing the IO operations to load files from 1...N processing time by parsing those files and their respective require calls memory allocation since require will cache all loaded files

Workaround

Some developers can inspect your code and conclude that they can load the expected dependency like this:

const feature1 = require('your-package/lib/feature1')
...

That works, but not just it is a minority of the users that do that, but they can run into problems if for some reason you decide to change a file name. For example:

module.exports = {
  feature1: require('./lib/feature1-optimized'),
 ...
  featureN: require('./lib/featureN')
}

Proposed Solution

const lazy = require('@danielsan/node-lazy-loader')(module)

module.exports = {
  get feature1 () { lazy(this, 'feature1', './lib/feature1') },
  get feature2 () { lazy(this, 'feature2', './lib/feature2') },
  get feature3 () { lazy(this, 'feature3', './lib/x', 'someExportedPropertyFromX') },
 ...
  get featureN () { lazy(this, 'featureN', './lib/featureN') }
}

By using getters the request will only happen when the property is read and this very package will rewrite the getter into a property with the expected value avoiding a function call the next time it is read

For example:

// your-module index.js
const lazy = require('@danielsan/node-lazy-loader')(module)

module.exports = {
  get feature1 () {
    console.log('reading feature1')
    lazy(this, 'feature1', './lib/feature1')
  }
}
// app index.js
const yourMod = require('your-module')

// the following line when executed will print 'reading feature1' then return the value
const a = yourMod.feature1

// the following line will only return the value since it is not longer a getter but a property
const b = yourMod.feature1

Security

If you are not comfortable with passing module to a 3rd-party function you can use it as follows:

const lazyCall = require('@danielsan/node-lazy-loader')({ require: module.require })