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

typescript-memoize

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-memoize

Memoize decorator for Typescript

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is typescript-memoize?

The `typescript-memoize` package provides decorators for memoizing methods in TypeScript. Memoization is a technique used to speed up function calls by caching the results of expensive function calls and returning the cached result when the same inputs occur again.

What are typescript-memoize's main functionalities?

Basic Memoization

This feature allows you to memoize a method so that the result is cached based on the input parameters. The second call with the same input will return the cached result without recomputing.

class Example {
  @Memoize()
  computeExpensiveValue(input: number): number {
    console.log('Computing...');
    return input * 2; // Simulate an expensive computation
  }
}

const example = new Example();
console.log(example.computeExpensiveValue(5)); // Computing... 10
console.log(example.computeExpensiveValue(5)); // 10 (cached result)

Custom Cache Key

This feature allows you to specify a custom cache key for the memoization. This can be useful if you need more control over how the cache keys are generated.

class Example {
  @Memoize((input: number) => `key-${input}`)
  computeExpensiveValue(input: number): number {
    console.log('Computing...');
    return input * 2; // Simulate an expensive computation
  }
}

const example = new Example();
console.log(example.computeExpensiveValue(5)); // Computing... 10
console.log(example.computeExpensiveValue(5)); // 10 (cached result)

Expiration Time

This feature allows you to set a time-to-live (TTL) for the cached result. After the TTL expires, the cached result will be invalidated, and the method will be recomputed.

class Example {
  @Memoize({ ttl: 5000 })
  computeExpensiveValue(input: number): number {
    console.log('Computing...');
    return input * 2; // Simulate an expensive computation
  }
}

const example = new Example();
console.log(example.computeExpensiveValue(5)); // Computing... 10
setTimeout(() => {
  console.log(example.computeExpensiveValue(5)); // Computing... 10 (cache expired)
}, 6000);

Other packages similar to typescript-memoize

Keywords

FAQs

Package last updated on 09 Dec 2016

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