Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Use a WeakMap to store memoized values #298

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Use a WeakMap to store memoized values #298

wants to merge 3 commits into from

Conversation

rtsao
Copy link
Member

@rtsao rtsao commented Sep 19, 2018

Using a WeakMap to store memoized values obviates the need for having an actual property on context.

@rtsao rtsao added the feature label Sep 19, 2018
@codecov
Copy link

codecov bot commented Sep 19, 2018

Codecov Report

Merging #298 into master will decrease coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #298      +/-   ##
==========================================
- Coverage   91.91%   91.87%   -0.04%     
==========================================
  Files          18       18              
  Lines         408      406       -2     
  Branches       76       75       -1     
==========================================
- Hits          375      373       -2     
  Misses         19       19              
  Partials       14       14
Impacted Files Coverage Δ
src/plugins/timing.js 85.29% <ø> (-0.43%) ⬇️
src/memoize.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10934e4...612a16d. Read the comment docs.

Copy link
Contributor

@ganemone ganemone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a breaking change because we would not be able to memoize primitives. I think it is actually better to use the approach with a map.

@rtsao

This comment has been minimized.

src/memoize.js Outdated

/**
* There is only ever a single ctx object in the browser.
* Therefore we can use a simple memoization function.
Copy link
Member Author

@rtsao rtsao Sep 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure if this assumption is valid. This is certainly the case in real applications, but I wonder if this may not be true with simulation tests that run in the browser (although I don't think this is a thing).

But if this assumption is not valid, we should add a test case for this.

@ganemone

This comment has been minimized.

ganemone
ganemone previously approved these changes Sep 19, 2018
@rtsao

This comment has been minimized.

src/memoize.js Outdated
return browserMemoize(fn);
}

const wm = new WeakMap();
Copy link
Contributor

@lhorie lhorie Sep 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put this logic be in an else block, and/or do feature detection in browser

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we ship core-js polyfills when necessary, so perhaps we can just assume WeakMap exists.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polyfill doesn't GC though, does it?

Copy link
Member Author

@rtsao rtsao Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but it the core-js polyfill seems to use this weak collection implementation (which I believe should allow for GC): https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/collection-weak.js

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants