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

@aspectjs/memo

v0.5.3

Published

Memoize methods with a simple annotation

Downloads

27

Readme

@aspectjs/memo

@aspectjs/memo is a project that enables memoization of methods using a simple @Memo() annotation.

📜 Memoization

Memoization is a technique used in computer programming to optimize the execution time of a function by caching its results for a given set of inputs. When a function is memoized, the first time it is called with specific arguments, the result is computed and stored in memory. Subsequent calls to the same function with the same arguments retrieve the cached result instead of re-executing the function.

main.ts

import { getWeaver } from '@aspectjs/core';
import { Memo, MemoAspect } from '@aspectjs/memo';

getWeaver().enable(new MemoAspect());

export class Demo {
  static fibonacci(num: number) {
    if (num < 2) {
      return num;
    } else {
      return Math.fibonacci(num - 1) + Math.fibonacci(num - 2);
    }
  }
  @Memo()
  static memoizedFibonacci(num: number) {
    if (num < 2) {
      return num;
    } else {
      return Math.memoizedFibonacci(num - 1) + Math.memoizedFibonacci(num - 2);
    }
  }
}

function main() {
  let [time, result] = getExecutionTime(() => Demo.fibonacci(40));
  console.log(`Demo.fibonacci(40) returned ${result} after ${time} ms`);
  // Demo.fibonacci(40) returned 102334155 after 999 ms

  [time, result] = getExecutionTime(() => Demo.memoizedFibonacci(40));
  console.log(`Demo.memoizedFibonacci(40) returned ${result} after ${time} ms`);
  // Demo.memoizedFibonacci(40) returned 102334155 after 12 ms

  [time, result] = getExecutionTime(() => Demo.memoizedFibonacci(40));
  console.log(`Demo.memoizedFibonacci(40) returned ${result} after ${time} ms`);
  // Demo.memoizedFibonacci(40) returned 102334155 after 0 ms
}

function getExecutionTime(fn: () => unknown) {
  let t = new Date().getTime();
  let result = fn();
  let elapsedTime = new Date().getTime() - t;
  return [elapsedTime, result];
}

main();

Memoization is based on the assumption that a function will produce the same result for the same set of inputs. By caching the result, subsequent calls can be avoided, reducing the computational overhead. This technique is particularly effective when a function is called multiple times with the same arguments, as it eliminates redundant computations.

🚀 Getting started

  • install the required packages
npm i @aspectjs/core @aspectjs/common @aspectjs/memo
  • Enable the MemoAspect

Memoization can be achieved by enabling the MemoAspect aspect. This aspect enables memoization by intercepting methods marked with the @Memo() annotation.

aop.ts

import { getWeaver } from '@aspectjs/core';
import { MemoAspect } from '@aspectjs/memo';

getWeaver().enable(new MemoAspect());
  • Annotate a method with the @Memo() annotation users.resource.ts

    import { Memo } from '@aspectjs/memo';
    
    export class UsersResource {
      @Memo()
      fetchOne(id: number) {
        console.log(`fetching user with id=${1}`);
        return fetch(`https://jsonplaceholder.typicode.com/users/${1}`).then(
          (r) => r.json(),
        );
      }
    }
  • Call the memoized method

    async function main() {
      const users = new UsersResource();
    
      console.log((await users.fetchOne(1)).name);
      console.log((await users.fetchOne(1)).name);
    }
    
    main();

    Output:

    fetching user with id=1
    # method UsersResource.fetchOne returned after 76ms
    Leanne Graham
    # method UsersResource.fetchOne returned after 0ms
    Leanne Graham