Skip to content

Implement WeakMap cache for frequent operations on data structures #5

@StarScape

Description

@StarScape

There are certain operations on Droplet's core data structures (paragraphs, runs, docs) that are extremely common and at least mildly expensive to compute. For example, text-len. We do not want to pre-calculate and store this value every time we instantiate e.g. a paragraph. It would be nice if instead we had a way of lazily calculating the value the first time it was needed, and then storing it the result, similar to this idiom in JS:

class Foo {
  get someValue() {
    if (this.__someValue !== undefined) {
      this.__someValue = someExpensiveComputation()
    }
    return this.__someValue
  }
}

Turns out you can solve this by use a cache based on a WeakMap of objects. Implement a weak-cache helper and cacheify any and all operations like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions