mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
feat: add reactive()
This commit is contained in:
parent
7bad215560
commit
bd23d54374
26 changed files with 1129 additions and 569 deletions
|
|
@ -58,7 +58,9 @@ for _, percent in { 0.1, 0.5, 1 } do
|
|||
bench.profilebegin("init")
|
||||
out()
|
||||
bench.profileend()
|
||||
|
||||
setSrc(updatePercentage(percent))
|
||||
|
||||
bench.profilebegin("update")
|
||||
out()
|
||||
bench.profileend()
|
||||
|
|
@ -71,7 +73,9 @@ for _, percent in { 0.1, 0.5, 1 } do
|
|||
bench.profilebegin("init")
|
||||
out()
|
||||
bench.profileend()
|
||||
|
||||
setSrc(updatePercentage(percent))
|
||||
|
||||
bench.profilebegin("update")
|
||||
out()
|
||||
bench.profileend()
|
||||
|
|
|
|||
|
|
@ -1,54 +0,0 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
|
||||
local CharmSync = require(ReplicatedStorage.packages["charm-sync"].src)
|
||||
|
||||
local patch = CharmSync.patch
|
||||
|
||||
local USERS = 1000
|
||||
|
||||
local function generate()
|
||||
local users = {}
|
||||
|
||||
for user = 1, USERS do
|
||||
users[user] = {
|
||||
coins = math.random(1, 2),
|
||||
gems = math.random(1, 2),
|
||||
}
|
||||
end
|
||||
|
||||
return { users = users }
|
||||
end
|
||||
|
||||
local function update(state)
|
||||
state = table.clone(state)
|
||||
state.users = table.clone(state.users)
|
||||
|
||||
for user = 1, USERS do
|
||||
local rng = math.random()
|
||||
|
||||
if rng < 1 / 3 then
|
||||
continue
|
||||
elseif rng < 2 / 3 then
|
||||
state.users[user] = nil
|
||||
else
|
||||
state.users[user] = {
|
||||
coins = math.random(1, 2),
|
||||
gems = math.random(1, 2),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return {
|
||||
ParameterGenerator = function()
|
||||
local state = generate()
|
||||
return state, update(state)
|
||||
end,
|
||||
Functions = {
|
||||
diff = function(_, a, b)
|
||||
patch.diff(a, b)
|
||||
end,
|
||||
},
|
||||
}
|
||||
87
benches/reactive.luau
Normal file
87
benches/reactive.luau
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
local Charm = require("../packages/charm/src")
|
||||
local bench = require("./bench")
|
||||
|
||||
local effect = Charm.effect
|
||||
local reactive = Charm.reactive
|
||||
|
||||
do
|
||||
local value = reactive({ a = 1 })
|
||||
bench.run("track reactive property", function()
|
||||
effect(function()
|
||||
local _ = value.a
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
do
|
||||
local value = reactive({ a = 1 })
|
||||
bench.run("write reactive property", function()
|
||||
value.a += 1
|
||||
end)
|
||||
end
|
||||
|
||||
for _, amount in { 10, 100, 1000 } do
|
||||
print("---")
|
||||
|
||||
do
|
||||
local rawArray = table.create(amount, 1)
|
||||
local array = reactive(rawArray)
|
||||
|
||||
bench.run(`track for loop, {amount} elements`, function()
|
||||
local sum = 0
|
||||
effect(function()
|
||||
for i = 1, #array do
|
||||
sum += array[i]
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
do
|
||||
local rawArray = table.create(amount, 1)
|
||||
local array = reactive(rawArray)
|
||||
|
||||
bench.run(`track iteration, {amount} elements`, function()
|
||||
local sum = 0
|
||||
effect(function()
|
||||
for _, x in array do
|
||||
sum += x
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
do
|
||||
local rawArray = table.create(amount, 1)
|
||||
local array = reactive(rawArray)
|
||||
|
||||
effect(function()
|
||||
local sum = 0
|
||||
for _, x in array do
|
||||
sum += x
|
||||
end
|
||||
end)
|
||||
|
||||
bench.run(`track index mutation (1st only), {amount} elements`, function()
|
||||
array[1] += 1
|
||||
end)
|
||||
end
|
||||
|
||||
do
|
||||
local rawArray = table.create(amount, 1)
|
||||
local array = reactive(rawArray)
|
||||
|
||||
effect(function()
|
||||
local sum = 0
|
||||
for _, x in array do
|
||||
sum += x
|
||||
end
|
||||
end)
|
||||
|
||||
bench.run(`track index mutation (all), {amount} elements`, function()
|
||||
for i = 1, #array do
|
||||
array[i] += 1
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue