perf: use next iteration

This commit is contained in:
Richard 2024-07-10 15:09:00 -07:00
parent ce5ae96c2b
commit 2e52d1bdae
7 changed files with 15 additions and 15 deletions

View file

@ -18,7 +18,7 @@ return function()
nested_atom = { value = atom(0), expected = false },
}
for key, case in cases do
for key, case in next, cases do
local success, result = pcall(function()
validate(case.value, key)
end)

View file

@ -15,7 +15,7 @@ local function atom<T>(state: T, options: AtomOptions<T>?): Atom<T>
local function atom(...)
if select("#", ...) == 0 then
for set in store.capturing do
for set in next, store.capturing do
set[atom] = true
end

View file

@ -36,7 +36,7 @@ local function mapped<K0, V0, K1, V1>(molecule: Molecule<{ [K0]: V0 }>, mapper:
local mappedKeys = {}
-- TODO: Only call mapper if the item has changed.
for key, item in items do
for key, item in next, items do
local newItem, newKey = mapper(item, key)
if newKey == nil then
newKey = key :: any
@ -48,7 +48,7 @@ local function mapped<K0, V0, K1, V1>(molecule: Molecule<{ [K0]: V0 }>, mapper:
end
end
for key in prevMappedItems do
for key in next, prevMappedItems do
if mappedKeys[key] == nil and mappedItems[key] == prevMappedItems[key] then
mappedItems[key] = nil
end

View file

@ -36,7 +36,7 @@ local function client(options: ClientOptions): ClientSyncer
for index = 1, select("#", ...) do
local payload = select(index, ...)
for key, state in payload.data do
for key, state in next, payload.data do
if payload.type == "patch" then
atoms[key](patch.apply(atoms[key](), state))
else

View file

@ -13,7 +13,7 @@ end
local function diff(prevState: { [any]: any }, nextState: { [any]: any })
local patches = table.clone(nextState)
for key, previous in prevState do
for key, previous in next, prevState do
local next = nextState[key]
if previous == next then
@ -26,7 +26,7 @@ local function diff(prevState: { [any]: any }, nextState: { [any]: any })
end
if _G.__DEV__ then
for key, value in prevState do
for key, value in next, prevState do
validate(value, key)
end
@ -50,7 +50,7 @@ local function apply(state: any, patches: any): any
local nextState = table.clone(state)
local stateIsArray = state[1] ~= nil
for key, patch in patches do
for key, patch in next, patches do
-- Diff-checking an array produces a sparse array, which will not be
-- preserved when converted to JSON. To prevent this, we turn string
-- keys back into numeric keys.

View file

@ -72,7 +72,7 @@ local function server(options: ServerOptions): ServerSyncer
local function getSnapshot()
local snapshot: { [string]: any } = {}
for key, atom in options.atoms do
for key, atom in next, options.atoms do
snapshot[key] = atom()
end
return snapshot
@ -105,7 +105,7 @@ local function server(options: ServerOptions): ServerSyncer
-- Populate the initial state and snapshot for each atom.
-- Subscribe to each atom and update the state when it changes.
for key, atom in options.atoms do
for key, atom in next, options.atoms do
cleanups[key] = subscribe(atom, function(current, previous)
pushSnapshot(key, current, previous)
changed = true
@ -122,7 +122,7 @@ local function server(options: ServerOptions): ServerSyncer
local payloads: { SyncPayload } = {}
local lastSnapshot
for index, snapshot in snapshots do
for index, snapshot in next, snapshots do
lastSnapshot = snapshot
if index == 1 then
@ -138,14 +138,14 @@ local function server(options: ServerOptions): ServerSyncer
snapshots = { lastSnapshot }
changed = false
for _, player in Players:GetPlayers() do
for _, player in next, Players:GetPlayers() do
callback(player, unpack(payloads))
end
end, options.interval or 0)
return function()
disconnect()
for _, cleanup in cleanups do
for _, cleanup in next, cleanups do
cleanup()
end
end
@ -170,7 +170,7 @@ local function server(options: ServerOptions): ServerSyncer
local payloads: { SyncPayload } = {}
for index, snapshot in snapshots do
for index, snapshot in next, snapshots do
if index == 1 then
continue
end

View file

@ -8,7 +8,7 @@ local function isUnsafeTable(object: { [any]: any })
local objectSize = 0
-- All keys must have the same type
for key in object do
for key in next, object do
local currentType = type(key)
if not keyType and SAFE_KEYS[currentType] then