Adjust signal again

This commit is contained in:
Schluffe
2026-08-28 10:31:39 +02:00
parent 7b3c9865fe
commit 51c1fc249d
2 changed files with 12 additions and 13 deletions
+8 -4
View File
@@ -1,11 +1,15 @@
import { Bench } from "tinybench"; import { Bench } from "tinybench";
const bench = new Bench({ name: "", time: 1000 }); const bench = new Bench({ name: "bench", time: 1000 });
let sink = 0; let sink = 0;
bench.add("", () => { bench
sink += 1; .add("a", () => {
}); sink++;
})
.add("b", () => {
sink++;
});
await bench.run(); await bench.run();
+4 -9
View File
@@ -4,22 +4,17 @@ let current: Current = null;
export function createSignal<T>(value: T): [() => T, (value: T) => void] { export function createSignal<T>(value: T): [() => T, (value: T) => void] {
let innerValue = value; let innerValue = value;
const observers: Array<Current> = []; const observers = new Set<() => void>();
const indices = new Map<Current, number>();
const getter = () => { const getter = () => {
if (current !== null && !indices.has(current)) { current !== null && observers.add(current);
observers.push(current);
indices.set(current, observers.length);
}
return innerValue; return innerValue;
}; };
const setter = (value: T) => { const setter = (value: T) => {
innerValue = value; innerValue = value;
for (let i = 0; i < observers.length; i++) { for (const observer of observers) {
observers[i]!(); observer();
} }
}; };