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

@wjsc/hold-on

v1.0.5

Published

Returns a function execution result or a cached version of it

Downloads

84

Readme

Current Version NPM Minified size Github Code Size Downloads/Year Issues License Contributors

NPM

Hold-on

Use case

This package can be used in this scenario

  1. You have a costly function: time consuming, heavy CPU or IO usage
  2. You need to perform that function frequently
  3. The result of your function can change over time
  4. You can tolerate some -configurable- inconsistency
  5. You want to optimize that process

How it works

It stores in memory the result of your function for immediate access, and clears that memory after a specified time. It returns a function that can be used instead your original one.

const hold = require('@wjsc/hold-on');
const myOptimizedFunction = hold(<Your Function>, <Time in miliseconds>);
myOptimizedFunction();

Usage

1. First example

const hold = require('@wjsc/hold-on');

// Define your costly function: Let's supose it's so heavy!
const myFunction = () => new Date(); 

// Make a new version of your function with 500 ms cache
const myOptimizedFunction = hold(myFunction, 500);

// This code will execute new Date() only once
for(let i = 0; i<50; i++){
    // And it prints always the same date
    console.log(myOptimizedFunction());
}

2. Second example: Retrieving a remote resource

const hold = require('@wjsc/hold-on');
// Any HTTP client
const fetch = require('node-fetch');

const myFunction = () => fetch('https://httpstat.us/200')
                         .then(res => res.text());
const myOptimizedFunction = hold(myFunction, 5000);

// This code will execute the HTTP GET only once
for(let i = 0; i<50; i++){
    myOptimizedFunction()
    .then(console.log);
}
// If you call the function after 5000 ms
// the request will be executed again

3. Third example: Cache file from local storage

const hold = require('@wjsc/hold-on');
const fs = require('fs');
const myFunction = () => new Promise((resolve, reject) => {
    fs.readFile('./my-file', 'utf8', (err, data) => 
        err ? reject(err) : resolve(data)
    )
})
const myOptimizedFunction = hold(myFunction, 5000);
myOptimizedFunction().then(console.log);

4. Fourth example: It's also great to cache a file from a remote Storage such as S3

const hold = require('@wjsc/hold-on');
const aws = require('aws-sdk');
aws.config.update({ 
    secretAccessKey: 'ABCDE',
    accessKeyId: '12345'

})
const s3 = new aws.S3();

const myFunction = () => {
    return new Promise((resolve, reject) => {
        s3.getObject({
            Bucket: 'my-bucket',
            Key: 'my-file'
        }, (err, data) => {
            if ( err ) reject(err)
            else resolve(data.Body.toString())
        })
    })
}
const myOptimizedFunction = hold(myFunction, 5000);
myOptimizedFunction().then(console.log);

100% Tested Library

Every line of code is tested https://github.com/wjsc/hold-on/blob/master/test/index.test.js

Tiny size

Less than 20 lines of code and no dependencies

Advanced

How to force termination

This function uses setTimeout to clear the internal cache. In some cases, you may need to clear this timer. This can be usefull if you are running a script that doesn't end at desired time, or if you want to terminate a background timer.

const myFunction = () => {};
const myOptimizedFunction = hold(myFunction, 100000000);
clearInterval(myOptimizedFunction.interval);

How to clear the memory cache

Just use the original function, or create a new function version.

Package name reference: https://www.youtube.com/watch?v=WPnOEiehONQ