Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    A Review of ES6 - Map/Set & WeakMap/WeakSet, Typed Arrays

    Published Oct 12, 2019 [  Node.js  ]

    Map/Set & WeakMap/WeakSet

    Set Data-Structure

    Cleaner data-structure for common algorithms based on sets.

    let s = new Set()
    s.add("hello").add("goodbye").add("hello")
    s.size === 2
    s.has("hello") === true
    for (let key of s.values()) // insertion order
        console.log(key)
    

    Map Data-Structure

    Cleaner data-structure for common algorithms based on maps.

    let m = new Map()
    let s = Symbol()
    m.set("hello", 42)
    m.set(s, 34)
    m.get(s) === 34
    m.size === 2
    for (let [ key, val ] of m.entries())
    

    Memory-leak-free Object-key’d side-by-side data-structure.

    let isMarked     = new WeakSet()
    let attachedData = new WeakMap()
    
    export class Node {
        constructor (id)   { this.id = id                  }
        mark        ()     { isMarked.add(this)            }
        unmark      ()     { isMarked.delete(this)         }
        marked      ()     { return isMarked.has(this)     }
        set data    (data) { attachedData.set(this, data)  }
        get data    ()     { return attachedData.get(this) }
    }
    
    let foo = new Node("foo")
    
    JSON.stringify(foo) === '{"id":"foo"}'
    foo.mark()
    foo.data = "bar"
    foo.data === "bar"
    JSON.stringify(foo) === '{"id":"foo"}'
    
    isMarked.has(foo)     === true
    attachedData.has(foo) === true
    foo = null  /* remove only reference to foo */
    attachedData.has(foo) === false
    isMarked.has(foo)     === false
    

    Typed Arrays

    Typed Arrays

    Support for arbitrary byte-based data structures to implement network protocols, cryptography algorithms, file format manipulations, etcc.

    //  ES6 class equivalent to the following C structure:
    //  struct Example { unsigned long id; char username[16]; float amountDue }
    class Example {
        constructor (buffer = new ArrayBuffer(24)) {
            this.buffer = buffer
        }
        set buffer (buffer) {
            this._buffer    = buffer
            this._id        = new Uint32Array (this._buffer,  0,  1)
            this._username  = new Uint8Array  (this._buffer,  4, 16)
            this._amountDue = new Float32Array(this._buffer, 20,  1)
        }
        get buffer ()     { return this._buffer       }
        set id (v)        { this._id[0] = v           }
        get id ()         { return this._id[0]        }
        set username (v)  { this._username[0] = v     }
        get username ()   { return this._username[0]  }
        set amountDue (v) { this._amountDue[0] = v    }
        get amountDue ()  { return this._amountDue[0] }
    }
    
    let example = new Example()
    example.id = 7
    example.username = "John Doe"
    example.amountDue = 42.0
    

    References: