Giter Club home page Giter Club logo

data-structures-javascript-notes's Introduction

Map

  • Map is like an object, but the main difference is that it allows keys of any type.

  • Methods, set, get, has, delete, clear, size

  • Looping over a map: keys, values or entries.

  • Map has a forEach method built-in.

  • Init a map with an array of key value pairs: new Map([['1', 'str1'], [1, 'num1'], [true, 'bool1']])

  • To turn an object into a Map, we can use Object.entries, which returns an array of key/value pairs: const map = new Map(Object.entries(obj));.

  • Object.fromEntries, given an array of key/value paires, creates an object from it, we can use it to turn a Map into an object. const obj = Object.fromEntries(map.entries()/map);.

Set

  • Set of values, no keys, where each value may only exist once, unique.

  • Create via new Set().

  • Methods: add, delete, has, clear, size

  • Only works with values.

WeakMap and Weakset

WeakSet can only contain objects, not primitive values. Like WeakMap, the "weakness" refers to the fact that the presence of an object in a WeakSet does not prevent the object from being garbage collected. If there are no other references to an object stored in a WeakSet, that object can be cleaned up by the garbage collector.

WeakMap can only have objects as keys. The reason for this limitation is tied to how garbage collection works in JavaScript. The "weakness" (i.e., the ability to be garbage collected) applies to the keys of the WeakMap. Primitive data types (like numbers or strings) are not collected in the same way as objects, so they can't be used as keys in a WeakMap.

Caching Expensive Function Results

Using a WeakMap for caching results of expensive functions based on objects is beneficial. This technique is especially useful when:

  • The function is computationally intensive: If the function takes considerable time or resources to compute a result based on an object, caching the result saves future computation time.
  • The input is object-based: When the function relies on objects as input, using these objects as keys in a WeakMap allows for quick retrieval of cached results without modifying the objects themselves.
  • Memory efficiency is a concern: WeakMap ensures that once the object is no longer needed (i.e., no other references exist), both the object and its associated cached result can be garbage collected, which helps in managing memory efficiently.

data-structures-javascript-notes's People

Contributors

gayashandeshapriya avatar

Watchers

 avatar

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.