mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 21:01:38 +00:00
feat: add reactive()
This commit is contained in:
parent
7bad215560
commit
bd23d54374
26 changed files with 1129 additions and 569 deletions
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