question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use WeakMap instead of mutating data with Symbol properties?

See original GitHub issue

Vega currently assigns Symbol(vega_id) properties to passed-in data. For example:

Screen Shot 2020-06-09 at 1 31 12 PM

Instead of mutating the passed-in data, Vega could use a WeakMap to associate a unique identifier with each element in the data set. This could be implemented like so in Tuple.js:

var tupleids = new WeakMap();

function tupleid(t) {
  return tupleids.get(t);
}

function setid(t, id) {
  tupleids.set(t, id);
  return t;
}

By avoiding mutation, Vega could use frozen data, and the API would be cleaner (in my opinion). For example, it would avoid these symbol properties appearing unexpectedly (and nondeterministically) when inspecting data in Observable notebooks, where side-effects are best avoided due to dataflow.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dantmancommented, Jun 25, 2020

I was just hit by this issue too. I wanted to use a Vega chart in a React component using data from a GraphQL API using the Apollo client. The data is queried with useQuery and useMemo memoizes a reordering of the data. Somewhere along this line the data is frozen, because this data is immutable and it is expected that nothing modifies it so in development at least the data is frozen to ensure this expectation is upheld.

1reaction
jheercommented, Jun 9, 2020

Thanks for the suggestion! We’ll look into it. At first glance this looks good to me, assuming there isn’t a significant performance hit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WeakMap - JavaScript - MDN Web Docs
Compared to using non-enumerable and/or Symbol properties, a WeakMap is external to the object and there is no way for user code to...
Read more >
Do we change states when setting new values in weakmaps?
In the real code I am using weakmaps to store private variables, with the purpose of making them inaccessible to the end-user. (abstraction)...
Read more >
JavaScript WeakMap and WeakSet - W3docs
In this chapter, we provide you with comprehensive information about useful means of storing data in JavaScript. Learn about WeakMap and Weakset here....
Read more >
Private properties in JavaScript - Curiosity driven
An instance of WeakMap is hidden inside a closure and indexed by Person instances. The values in the map are objects holding private...
Read more >
WeakMap and WeakSet - The Modern JavaScript Tutorial
WeakSet behaves similarly: ... Being “weak”, it also serves as additional storage. But not for arbitrary data, rather for “yes/no” facts. A ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found