Skip to content

Commit

Permalink
Test perf of Set based alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 4, 2024
1 parent 4e84f83 commit 57fa02b
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function createContext(defaultValue, contextId) {
/** @type {FunctionComponent} */
Provider(props) {
if (!this.getChildContext) {
/** @type {Component[] | null} */
let subs = [];
/** @type {Set<Component> | null} */
let subs = new Set();
let ctx = {};
ctx[contextId] = this;

Expand All @@ -31,23 +31,19 @@ export function createContext(defaultValue, contextId) {

this.shouldComponentUpdate = function (_props) {
if (this.props.value !== _props.value) {
subs.some(c => {
subs.forEach(c => {
c._force = true;
enqueueRender(c);
});
}
};

this.sub = c => {
subs.push(c);
subs.add(c);
let old = c.componentWillUnmount;
c.componentWillUnmount = () => {
if (subs) {
if (subs.length === 1) {
subs = [];
} else {
subs[subs.indexOf(c)] = subs.pop();
}
subs.delete(c);
}
if (old) old.call(c);
};
Expand Down

0 comments on commit 57fa02b

Please sign in to comment.