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

optimism

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optimism

Composable reactive caching with efficient invalidation.

  • 0.11.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.5M
decreased by-5.43%
Maintainers
1
Weekly downloads
 
Created

What is optimism?

The 'optimism' npm package is a library for memoizing asynchronous functions. It helps in caching the results of function calls and reusing them when the same inputs occur, thus improving performance by avoiding redundant computations.

What are optimism's main functionalities?

Memoizing Asynchronous Functions

This feature allows you to memoize asynchronous functions. The 'wrap' function from 'optimism' is used to wrap an async function, caching its results based on the input arguments. Subsequent calls with the same arguments will return the cached result instead of executing the function again.

const { wrap } = require('optimism');

const fetchData = wrap(async (url) => {
  const response = await fetch(url);
  return response.json();
});

// Usage
fetchData('https://api.example.com/data').then(data => console.log(data));
fetchData('https://api.example.com/data').then(data => console.log(data)); // This call will use the cached result

Custom Cache Key

This feature allows you to define a custom cache key for the memoized function. By providing a 'makeCacheKey' function, you can control how the cache key is generated based on the function's arguments.

const { wrap } = require('optimism');

const fetchData = wrap(async (url, params) => {
  const response = await fetch(url, { params });
  return response.json();
}, {
  makeCacheKey: (url, params) => `${url}-${JSON.stringify(params)}`
});

// Usage
fetchData('https://api.example.com/data', { id: 1 }).then(data => console.log(data));
fetchData('https://api.example.com/data', { id: 1 }).then(data => console.log(data)); // This call will use the cached result

Cache Expiration

This feature allows you to set an expiration time for the cache. By specifying the 'maxAge' option, you can control how long the cached result is valid. After the specified time, the cache will expire, and the function will be executed again to fetch new data.

const { wrap } = require('optimism');

const fetchData = wrap(async (url) => {
  const response = await fetch(url);
  return response.json();
}, {
  maxAge: 60000 // Cache expires after 60 seconds
});

// Usage
fetchData('https://api.example.com/data').then(data => console.log(data));
setTimeout(() => {
  fetchData('https://api.example.com/data').then(data => console.log(data)); // This call will fetch new data after cache expiration
}, 61000);

Other packages similar to optimism

Keywords

FAQs

Package last updated on 16 Sep 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