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

limitablemap-plus

v0.3.0

Published

The limitable map, for avoid memory leak issue.

Downloads

1

Readme

Limitable Map

The limitable map, for avoid memory leak issue.

模块起源

模块的起源来自于我发现JavaScript开发者喜欢利用JSON对象的键值对做缓存对象。

var map = {};

var get = function (key) {
  return map[key];
};

var set = function (key, value) {
  map[key] = value;
};

// 检查缓存
if (!get(key)) {
  // 从数据库或别的地方获取了对象后,放进缓存中
  set(key, value);
}

以上的代码会经常出现在前后端的开发中。这段代码是有潜在风险的。如果对于key不做任何限制,这个缓存对象将可能变得极大。因为将其当作缓存来使用,所以这个对象一直无法回收,当量达到一定的时候,那些不再使用的key,又不能得到回收,就可能造成内存泄漏。
对于字符串或者可以序列化为字符串的对象,通常利用外部缓存工具来完成,比如memcached或者redis来搞定。这些工具具有良好的内存大小控制和过期设置,无需担心内存占用和回收。
但是对于那些无法通过redis来缓存的对象,比如Buffer(为了节省字符串转换的开销),依然需要存放在内存中。这时候这个limitablemap将会帮助你做简单的限制,实现内存回收。

原理

limitable模块的实现原理十分简单,就是在内部维护一个键的数组(队列),当数组的大小到达限制(默认为10)后,将第一个键和键对应元素删除掉。

安装

npm install limitablemap-plus

使用方式

var LimitableMap = require('limitablemap');

var map = new LimitableMap();

map.set("key1", "key1");
map.get("key1");

// it is new api.
map.del('key1');
map.remove('key1');

map.size();
map.length();

使用的时候与普通map对象没有差别,API接口:set/get/remove(del)/size(length)

注意

在使用时,注意评估有效键的数量,如果设置过小,缓存的作用太小;设置过大,可能会浪费内存。设置限制值的方式如下:

var map = new LimitableMap(100);