Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wjsc/hold-on

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wjsc/hold-on

Returns a function execution result or a cached version of it

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

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');
const fetch = require('node-fetch); // Or any HTTP client

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

// This code will execute the HTTP GET only once
for(let i = 0; i<50; i++){
    console.log(myOptimizedFunction());
}
// After 500 ms the request will be executed again

3. Third example: It's also great to retrieve a file from a remote Storage such as S3
const hold = require('@wjsc/hold-on');
const aws = require('aws-sdk');
const s3 = new aws.S3();

const myFunction = () => s3.getObject({ Bucket: 'abc', Key: 'abc.txt' });
const myOptimizedFunction = hold(myFunction, 20000);

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

Keywords

FAQs

Package last updated on 20 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc