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
weakmap-polyfill
This package provides a polyfill for WeakMap for environments where it's not supported natively. It's similar to es6-weak-map but might have different performance characteristics or compatibility nuances.
weakmap
Another implementation of the WeakMap specification, offering similar functionality to es6-weak-map. It might differ in aspects such as API design, performance optimizations, or additional features.
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.get(obj);
map.has(obj);
map.delete(obj);
map.get(obj);
map.has(obj);
map.set(obj, "bar");
map.has(obj);
Tests
$ npm test