Socket
Socket
Sign inDemoInstall

es6-weak-map

Package Overview
Dependencies
9
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    es6-weak-map

ECMAScript6 WeakMap polyfill


Version published
Weekly downloads
5.6M
increased by3.34%
Maintainers
1
Install size
854 kB
Created
Weekly downloads
 

Package description

What is es6-weak-map?

The es6-weak-map npm package provides a polyfill for the ES6 WeakMap implementation. It allows for the creation of collections of key/value pairs where the keys are objects and the values can be arbitrary values. The key feature of WeakMaps is that they do not prevent garbage collection if there are no other references to the key object, making them useful for managing memory in large applications.

What are es6-weak-map's main functionalities?

Creating and using a WeakMap

This code demonstrates how to create a WeakMap, add a key/value pair to it, retrieve the value using the key, and then allow the key to be garbage collected by removing references to it.

{"var WeakMap = require('es6-weak-map');\nvar myWeakMap = new WeakMap();\nvar obj = {};\nmyWeakMap.set(obj, 'value');\nconsole.log(myWeakMap.get(obj)); // 'value'\nobj = null; // Now obj can be garbage collected if no other references exist"}

Checking for a key's presence and deleting a key

This example shows how to check if a WeakMap contains a certain key using the `has` method and how to remove a key/value pair from the WeakMap using the `delete` method.

{"var WeakMap = require('es6-weak-map');\nvar myWeakMap = new WeakMap();\nvar obj = {};\nmyWeakMap.set(obj, 'value');\nconsole.log(myWeakMap.has(obj)); // true\nmyWeakMap.delete(obj);\nconsole.log(myWeakMap.has(obj)); // false"}

Other packages similar to es6-weak-map

Readme

Source

Build status Windows status Transpilation status npm version

es6-weak-map

WeakMap collection as specified in ECMAScript6

Roughly inspired by Mark Miller's and Kris Kowal's WeakMap implementation.

Differences are:

  • Assumes compliant ES5 environment (no weird ES3 workarounds or hacks)
  • Well modularized CJS style
  • Based on one solution.

Limitations

  • Will fail on non extensible objects provided as keys

Installation

$ npm install es6-weak-map

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Usage

If you want to make sure your environment implements WeakMap, do:

require("es6-weak-map/implement");

If you'd like to use native version when it exists and fallback to polyfill if it doesn't, but without implementing WeakMap on global scope, do:

var WeakMap = require("es6-weak-map");

If you strictly want to use polyfill even if native WeakMap exists, do:

var WeakMap = require("es6-weak-map/polyfill");
API

Best is to refer to specification. Still if you want quick look, follow example:

var WeakMap = require("es6-weak-map");

var map = new WeakMap();
var obj = {};

map.set(obj, "foo"); // map
map.get(obj); // 'foo'
map.has(obj); // true
map.delete(obj); // true
map.get(obj); // undefined
map.has(obj); // false
map.set(obj, "bar"); // map
map.has(obj); // false

Tests

$ npm test

Keywords

FAQs

Last updated on 07 Jun 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc