Giter Club home page Giter Club logo

weakmap-polyfill's Introduction

weakmap-polyfill

NPM Version Build Status Bundle Size (minified) GitHub License

ECMAScript6 WeakMap polyfill.

Installation

npm

$ npm install --save weakmap-polyfill

Usage

Import or require weakmap-polyfill, then WeakMap will be defined in the global scope if native WeakMap is not supported in running environment.

using import

import 'weakmap-polyfill';
const weakMap = new WeakMap();

using require

require('weakmap-polyfill');
var weakMap = new WeakMap();

browser (standalone)

<script src="weakmap-polyfill.min.js"></script>
<script>
var weakMap = new WeakMap();
</script>

Compatibility

  • Chrome 15
  • Firefox 3
  • IE 7
  • Safari 4
  • Opera 11.5
  • Edge

Browser Tests

Limitations

This polyfill has following few limitations.

  • WeakMap iterable argument is not supported. (23.3.1.1 WeakMap ( [ iterable ] ))
  • Frozen and sealed objects are not supported.
  • The values held by a WeakMap can't be collected once the map itself is GCed, since the values here are tied to the keys. #4

License

MIT

weakmap-polyfill's People

Contributors

dependabot[bot] avatar polygonplanet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

weakmap-polyfill's Issues

Documenting limitations (frozen and sealed objects, values that outlive the WeakMap)

The approach taken by this lib is pretty good, since the GC lifetime of values is tied to that of keys, however, it has a few limitations that may be worth documenting:

  1. The lib doesn't work with frozen and sealed objects.
  2. The values held by a WeakMap can't be collected once the map itself is GCed, since the values here are tied to the keys.

A third thing, which is more of a suggestion:

Even if they are not enumerable, the keys can be obtained in some IE versions by using Object.getOwnPropertyNames. This could be mitigated relatively cheaply by storing the mapping inside a single object that is tied to the key object using a secret field as a value. You can then monkeypatch Object.getOwnPropertyNames to strip that secret field from the results and, also monkeypatch Object.getOwnPropertyNames if present (in IE thanks to a polyfill, not sure about other envs).

If this monkeypatch approach seems workable to you, you could also solve 1) by monkeypatching Object.freeze and Object.seal to add the secret field before freezing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.